When the program is running, variables are a good way to save data, but the data stored in variables, sequences, and objects are temporary and will be lost after the program ends. If you want the data to remain after the program ends, you need to save the data to a file in. Python provides built-in file objects and built-in modules for manipulating files and directories. With these technologies, data can be easily saved to files (such as text files).
Regarding files, it has two key attributes, namely "file name" and "path". The file name refers to the name set for each file, and the path is used to indicate the location of the file on the computer. For example, on my Windows 7 notebook, I have a file named projects.docx (the part after the period is called the "extension" of the file, which indicates the type of file), and its path is at D:\demo\exercise, also That is, the file is located in the exercise subfolder of the demo folder under the D drive.
The file name and path can be analysed. Project.docx is a Word document, and both demo and exercise refer to "folders" (also called directories). A folder can contain files and other folders, such as project.docx in the exercise folder, which in turn is in the demo folder.
Note that D:\ in the path refers to the "root folder", which contains all other folders. In Windows, the root folder is named D:\, also known as the D: drive. In OS X and Linux, the root folder is /. This tutorial uses a Windows-style root folder. If you are typing an example of an interactive environment on OS X or Linux, use / instead.
In addition, additional volumes, such as DVD drives or USB flash drives, appear differently on different operating systems. On Windows, they are represented as new, character-driven root drives. Such as D:\ or E:\. On OS X, they are represented as new folders under the / Volumes folder. On Linux, they are represented as new folders under the / mnt folder. Also note that while folder and file names are not case sensitive on Windows and OS X, they are case sensitive on Linux.
On Windows, path writing uses a backslash "\" as a separator between folders. But on OS X and Linux, use forward slashes "/" as their path separator. If you want your program to run on all operating systems, you must handle both cases when writing Python scripts.
Fortunately, it is easy to do this with the os.path.join() function. If you pass to it a single file and a string of folder names on the path, os.path.join() returns a string of file paths with the correct path separator. Enter the following code in the interactive environment:
import os os.path.join('demo', 'exercise')
The output is:
'demo\\exercise'
Because this program runs on Windows, os.path.join('demo', 'exercise') returns 'demo\\exercise' (note that there are two backslashes because each backslash requires escaped by another backslash character). If this function is called on OS X or Linux, the string will be 'demo/exercise'.
Not only that, if you need to create a file storage path with a file name, the os.path.join() function is also useful. For example, the following example adds names from a list of file names to the end of the folder name:
import os myFiles = ['accounts.txt', 'details.csv', 'invite.docx'] for filename in myFiles: print(os.path.join('C:\\demo\\exercise', filename))
The output is:
C:\demo\exercise\accounts.txt
C:\demo\exercise\details.csv
C:\demo\exercise\invite.docx
More Tutorials:
Python Installation - Linux (Ubuntu)More Python Exercises:
Python String Exercises