Jump to content

Applied Programming/Files

From Wikiversity

This lesson introduces files and file processing.

Objectives and Skills

[edit | edit source]

Objectives and skills for this lesson include:[1]

  • Construct and analyze code segments that perform file input and output operations
    • Open; close; read; write; append; check existence; delete; with statement
  • Construct and analyze code segments that perform console input and output operations
    • Read input from console; print formatted text; use of command line arguments

Readings

[edit | edit source]
  1. Wikipedia: Computer file
  2. Wikipedia: File system
  3. Wikipedia: Directory (computing)
  4. Wikipedia: Directory structure
  5. Wikipedia: Text file
  6. Wikipedia: Binary file

Multimedia

[edit | edit source]
  1. YouTube: File Systems
  2. Youtube: Reading and Writing to Files in Python
  3. Youtube: Reading and Writing to Files in Java
  4. YouTube: Writing Binary Files

Examples

[edit | edit source]

Activities

[edit | edit source]
  1. Review Wikipedia: Password strength. Create a program that asks the user for an input password. If your programming language or library supports it, get the input without echoing the characters as they are entered. Determine the entropy of the input password based on length of password and the number of different character sets used in the password (e.g. Entropy/Strength Test). Use a separate function to determine password entropy. Avoid using global variables by passing parameters and returning results. Include appropriate data validation and parameter validation. Add program and function documentation, consistent with the documentation standards for your selected programming language.
  2. Review Wikipedia: Dictionary attack. Enhance the program above by downloading a dictionary of English words as a text file: (i.e. GitHub) Use a separate function to check the password and see if it matches one of the dictionary words. Inform the user if their password is susceptible to a dictionary attack. Use exception handling for all file operations. Validate parameters and update program and function documentation, consistent with the documentation standards for your selected programming language.
  3. Enhance the program above by downloading a text file of common passwords: (i.e. GitHub) Use the function above to check the password and see if it matches a common password. Inform the user if their password susceptible to a common password attack. Validate parameters and update program and function documentation, consistent with the documentation standards for your selected programming language.
  4. Enhance the program above by saving all passwords entered by the user in a text file. Use the function above to check the password and see if it matches a previously entered password. Validate parameters and update program and function documentation, consistent with the documentation standards for your selected programming language. The final program will check password strength and validate passwords against an English dictionary, a common password list, and a recently-used password list.

Lesson Summary

[edit | edit source]
  • Python allows you to open files in four different modes: read ("r"), write ("w"), append ("a"), and create ("x").[2]
  • A computer's file system is a division of information and data into files and directories (folders).[3]
  • A file system consists of two or three layers. Sometimes the layers are explicitly separated, and sometimes the functions are combined.[3]
    • The logical file system is the first layer and is responsible for interaction with the user application.[3]
    • The second optional layer is the virtual file system which allows support for multiple concurrent instances of physical file systems.[3]
    • The third layer is the physical file system. This layer is concerned with the physical operation of the storage device and handles tasks such as buffering and memory management.[3]
  • Windows makes use of the FAT, NTFS, exFAT, Live File System and ReFS file systems while macOS uses a HFS Plus file system along with the term "Mac OS Extended".[3]
  • File systems include utilities to initialize, alter parameters of and remove an instance of the file system.[3]
    • Directory utilities may be used to create, rename and delete directory entries.[3]
    • File utilities create, list, copy, move and delete files, and alter metadata. They may be able to truncate data, truncate or extend space allocation, append to, move, and modify files in-place.[3]
    • Some of the most important features of file system utilities involve supervisory activities which may involve bypassing ownership or direct access to the underlying device.[3]
  • A directory-based file system is one where directories coordinate the organization and retrieval of information.[4]
    • Although rare, some embedded computers have no directories (everything is a file) or no ability to store directories inside other directories (thereby flattening the computer's storage).[4]
  • If one is referring to a container of documents, the term folder is more appropriate. The term directory refers to the way a structured list of document files and folders is stored on the computer.[4]
  • Current operating systems typically allow for long filenames, more than 250 characters per pathname element.[5]
  • A fully qualified filename is a textual string that includes the path of the file, as well as its unique identifier and extension (e.g., C:\Users\cklei\Desktop\hello_world.py).[5]
  • UNIX-like operating systems use the Filesystem Hierarchy Standard. All files and directories appear under the root directory "/", even if they are stored on different physical devices.[5]
  • On Windows, files get arranged in a hierarchical forest of trees, where each tree root is a "drive letter" labeling the memory space, such as C in the path C:\Program Files.[3]
  • Text files are employed for human-readable storage of information, notable for their simplicity.[6]
  • On Windows, a newline is signaled by the carriage return and line feed characters in unison (CRLF); on UNIX-like systems, including macOS devices, the newline is simply communicated by the line feed character.[6]
  • Binary files are usually imagined to be a series of bytes, which is a group of eight bits itself.[7]
    • They often represent things other than characters (otherwise, you'd likely opt for a text file).[7]
  • Binary files will sometimes be handled by mechanisms that can only deal with textual data; Base64 is an encoding scheme that makes such a translation possible.[7]
  • When two computers (or systems) can run the same executable, they are said to be 'binary compatible'.
    • Some software companies produce applications for Windows and the Macintosh that are binary compatible, which means that a file produced in a Windows environment is interchangeable with a file produced on a Macintosh. [7]
    • This also makes it possible to run programs built for deprecated versions of Windows on newer systems.[7]
  • A hex editor is specially designed to view binary files as chunks of hexadecimal (or decimal or binary) values. If a binary file is opened in a text editor, meanwhile, each group of eight bits is usually translated into a single character.[7]
  • When the 'with' keyword is used together with the open() method in Python, the file doesn't need to be closed. It will close automatically after the execution of the 'with' statement.[8]

Key Terms

[edit | edit source]
absolute path
A full path or location of a file, including the root directory, that points to the same location in a file system regardless of current working directory.[9]
binary file
A file that is non-textual—a raw sequence of bytes.[7]
directory structure
Files are stored in a hierarchical tree structure as the childless leaves of directories (or folders).[5]
directory
A structure that contains references to other computer files and possibly other subdirectories.[4]
file system
The way in which data is stored and retrieved on the machine.[3]
file utilities
File utilities allow users to create, list, copy, move and delete files, and alter metadata.[3]
fully-qualified filename
A string that uniquely identifies a file stored on the computer by including the path, name, and extension of the file.[5]
metadata
Information that is typically associated with each file within a file system. File systems might store the file creation time, the time it was last accessed, etc.[3]
parent & children
A parent directory houses "children" files or subdirectories. A child is a file or subdirectory housed in a parent directory.[4]
path
The location of a file among the hierarchy of directories (possibly indicating a storage device, as well).[4]
relative path
A path or location of a file that starts from the current working directory.
root
The highest-level directory in the file system hierarchy, found in UNIX-like operating systems.[10]
text file
A file that is structured as a sequence of lines of electronic text, it exists stored as data within a computer file system.[6]

See Also

[edit | edit source]

References

[edit | edit source]