We know that filenames, directory names, and link names use a string as their identifier, but given us an identifier, how do we determine whether it refers to a regular file filename, directory name, or link name? At this point, we can use the Isfile function provided by the Os.path module, The Isdir function and the Islink function to achieve our goal are as follows:
Print MyFile, ' is a ',
If Os.path.isfile (myfile):
print ' Plain file '
If Os.path.isdir (myfile):
print ' directory '
If Os.path.islink (myfile):
print ' link '
You can also find the date and size of the file:
time_of_last_access = Os.path.getatime (myfile)
Time_of_last_modification = Os.path.getmtime (myfile)
Size = Os.path.getsize (myfile)
The time here is in seconds and starts from January 1, 1970. In order to get the last access date in days, you can use the following code:
Import Time # Time.time () returns the current date
Age_in_days = (Time.time ()-time_of_last_access)/(60*60*24)
In order to obtain the details of the file, you can use the Os.stat function and other utilities in the stat module to achieve the purpose, as follows:
Import Stat
Myfile_stat = Os.stat (myfile)
Size = Myfile_stat[stat. St_size]
mode = Myfile_stat[stat. St_mode]
If Stat. S_isreg (Mode):
print '% (myfile) is a regular file with a size of% d bytes '%\
VARs ()
For more information about the stat module, see Python Library Reference. To test the read, write, and execute permissions for a file, you can use the Os.access function, as follows:
If Os.access (myfile, OS. W_OK):
Print myfile, ' have Write permission '
If Os.access (myfile, OS. R_OK | Os. W_OK | Os. X_OK):
Print myfile, ' have read, write, and Execute permissions '
Http://tech.it168.com/a2009/0708/602/000000602694_1.shtml
In many applications, file manipulation is a basic feature and is an important part of it. Python is a very simple file operation relative to other languages
Read and write:
Reading the data from the text and writing the data into the text is the basic operation of the text. This is very simple. We open a text ready to write the data:
fp = open ("Test.txt", "W")
"W" indicates that we are going to write the data into text. The rest is easier to understand. The next step is to write the data into the text:
Fp.write (' This was a test. \nready, it is. ‘ )
This puts the string "This is a test." Write in the first line of the text, "really, it is." Write to the second line. Finally, we need to clean and close the text.
Fp.close ()
As you can see, this is easy, especially for Python objects. But be clear that when you use the "W" mode to write the data again into the text, everything in the text will be deleted. To solve this problem, use the "a" pattern to append the data to the end of the text and add the data to the end:
fp = open (' Test.txt ', ' a ')
Fp.write (' \n\n\nbottom line. ‘ )
Fp.close ()
Now we're going to read the text and display the data:
fp = open (' Test.txt ')
Print Fp.read ()
Fp.close ()
All the data in the text is read out and displayed. We can also read a line of data in the text:
fp = open (' Test.txt ')
Print fp.readline () # "This is a test."
It is also possible to store all the lines in the text in a list:
fp = open (' Test.txt ')
FileList = Fp.readlines ()
For Fileline in FileList:
print ' >> ', fileline
Fp.close ()
When reading data from text, Python remembers the position of the pointer in the text, as in the following example:
fp = open (' Test.txt ')
garbage = Fp.readline ()
Fp.readline () # "Really, it is."
Fp.close ()
Only the second line is shown. Of course, we can also read the data by positioning the pointer somewhere else.
fp = open (' Test.txt ')
garbage = Fp.readline ()
FP. Seek (0)
Print fp.readline () # "This is a test."
Fp.close ()
As can be known from the above example, we can tell Python to continue reading the data from the first byte of the text. Therefore, the first row of data is printed out. We can also ask Python to tell the pointer the current position:
fp = open (' Test.txt ')
Print Fp.readlien () # "This is a test"
Print FP. Tell (0) # "17"
Print fp.readline () # "Really, it is"
Similarly, you can read data for a specified number of bytes at a time:
fp = open (' Test.txt ')
Print FP (1) # "T"
Fp.seek (4)
Print Fp.read (1) # "T"
When we are on the Windows and Macintosh platforms, sometimes it is possible to write data in binary mode, compared to slice files. To do this, simply open the text in "B" mode:
fp = open (' TestBinary.txt ', ' WB ')
Fp.write (' There is no spoon. ‘ )
Fp.close ()
fp = open (' TestBinary.txt ', ' RB ')
Print Fp.read ()
FP. Close ()
Talk about file operations and directory operations in Python