python learning notes 9: file and directory operations

catalogue

1. writing documents

Example 1: Recommended Practice: with... as: it is not necessary to close the file manually;

>>> with open('tmp.txt', 'w') as fh: #Use with to open. After using fh, you do not need to close it manually;  
...     fh.write('line0\n') # Need to add line breaks manually  
...     fh.write('line1\n')  
...  

Example 2: as a general practice, open the file, write the file, and close the file; It is recommended to use the method of with, which does not need to close the file manually

>>> fh = open('tmp.txt', 'w')  
>>> fh.write('line0\n') # Note that line breaks will not occur automatically, and line breaks need to be added manually;  
>>> fh.write('line1\n')  
>>> fh.close() # Need to close after use  

2. read the file

Example 1: recommended practice, with... as: no need to close the file manually

>>> with open('tmp.txt', 'r') as fh: #To open a file using with, you do not need to close the file manually
...     for line in fh:  
...         print(line, end='') # There is no line break at the end of the line, or print(line.strip('\n'))  
...  
Output:  
line0  
line1  

If the file to be opened contains Chinese, it will result in a report of unicodedecodeerror:'gbk'codec can't decode byte..., in this case, you can add an edit parameter to open: open ('tmp.txt','r', encoding='utf-8')

(obtain line number and line text at the same time: for lineno, line in enumerate(fh, 1))

Example 2: general practice: open the file, read the file, and close the file; Recommended practices for with

>>> fh = open('tmp.txt') #No open mode is declared. The default mode is read, that is, the second parameter is r by default
>>> while True:  
...     line = fh.readline()#readline, read a line  
...     if len(line) == 0:#If the length is equal to 0, it means the end of the file. Note that the length of the blank line is 1, and the newline character occupies a length  
...         break  
...     print(line)  
...  
>>> fh.close()  
   
Output:  
line0  
<Blank line> #Because print will add a newline character at the end by default  
line1  
<Blank line>  

Several read functions

>>> fh.read()       #Read the whole file and put it into a string variable;  
>>> fh.readline()   #Read only one line at a time and put it into the string;  
>>> fh.readlines()  #Read the entire file and analyze it into a list;  

Example 3:

>>> with open('tmp.txt', 'r') as fh:  
...     txt = fh.read() # 'line0\nline1\n', read all rows and return str;  

Example 4:

>>> with open('tmp.txt', 'r') as fh:  
...     txt0 = fh.readline()# 'line0\n', read one row at a time;  
...     txt1 = fh.readline().strip() # 'line1', read one line at a time, and str.strip() can remove line breaks and spaces at the end of the line;  

Example 5:

>>> with open('tmp.txt', 'r') as fh:  
...      list_txt = fh.readlines() # ['line0\n','line1\n'], read all rows and put them in the list;  

3. read the compressed file (.gz)

Read the gz file using the gzip module:

import gzip  
with gzip.open('file.txt.gz', 'rb') as fid: # Read mode is rb  
for line in fid:  
    line = line.decode() # The read line is in binary format (b''), which needs to be decoded  
    line = line.strip('\n') # Remove line breaks  
    print(line)  

4. mode of opening file

mode describe
r Open the file as read-only. The pointer to the file will be placed at the beginning of the file. This is the default mode.
w Open a file for writing only. If the file already exists, open the file and edit it from the beginning, that is, the original content will be deleted. If the file does not exist, create a new file.
a Open a file for appending. If the file already exists, the file pointer is placed at the end of the file. That is, the new content will be written after the existing content. If the file does not exist, create a new file to write.
rb Binary mode of r
wb Binary mode of w
ab Binary mode of a
r+ r (the nonexistent file will not be created. Writing from the top will overwrite the contents of the previous position. If reading before writing, the contents will be appended at the end of the file.)
w+ w read / write mode (if the file exists, the entire file will be overwritten; if it does not exist, it will be created. The writing will not be completed until it is close d)
a+ a's read / write mode (readable and writable, read content from the top of the file, add content from the bottom of the file, and create if it does not exist)
rb+ Binary + read / write mode of r
wb+ Binary + read / write mode of w
ab+ Binary + read / write mode of a

5. with... as statement

You can use the with statement instead of the try... finally statement. The advantage is that the syntax is relatively concise. See also section 15.2.
No matter whether an exception occurs during file processing, it can ensure that the open file handle is closed after the with statement is executed.

>>> with open(file, 'r') as f:
...     print(f.read())

Equivalent to the following:

>>> try:
...     f = open(file, 'r')
...     print(f.read())
... finally:
...     if f:
...         f.close()
...

6. document test / document judgment

1. judge whether the file / directory exists

>>> os.path.exists(test_file) # True | False
>>> os.path.exists(test_dir)# True | False

2. judge whether the file is a directory or an ordinary file

>>> os.path.isfile(test_target) # Test_ When target is a normal file, it returns True; when it is a directory or the directory does not exist, it returns False;
>>> os.path.isdir(test_target) # Test_ When target is a directory, it returns True; when it is a normal file or the file does not exist, it returns False;

3. judge whether the document is readable or writable

>>> os.access(test_file, os.F_OK) # File exists, return True;
>>> os.access(test_file, os.R_OK) # The file is readable and returns True;
>>> os.access(test_file, os.W_OK) # The file is writable and returns True;
>>> os.access(test_file, os.X_OK) # The file is executable and returns True;

7. file, directory and path

Get the directory where the program is located:

#Assume that the contents of the PY file /test/path/a.py are  
import os, sys  
print(f'os.getcwd() = {os.getcwd()}')  
print(f'sys.path[0] = {sys.path[0]}')  
  
#The user executes in the /home/g444054/ directory  
$ python /test/path/a.py  
os.getcwd() = /home/g444054 # Returns the path to execute the command   
sys.path[0] = /test/path    # The path of the script is returned   

List the files in the specified directory (when the specified directory is empty, the default is the current directory), and return the list. Note: the returned list element does not contain the corresponding path, but only the file name. To recurse directories, you need to use os Walk

>>> os.listdir(path)  
['file0', 'file1', 'dir0']  

Create directory, delete directory, delete file, copy file

>>> os.mkdir(path)# Create a directory;  
>>> os.rmdir(path)# To delete a directory recursively, use shutil Rmtree;  
>>> os.remove(file) # Delete the file;  
>>> os.rename('test.txt', 'test.py') # rename  
>>> shutil.copy('/home/g004440**/.cshrc', '/home/n004435**/') #Copy file  
>>> shutil.copytree('/home/g004440**/dir0', '/home/g004440**/dir.bak') #Copy directories and all internal files The destination folder cannot already exist  
>>> shutil.rmtree(path) # To delete a directory tree, you must point to a folder, not a symbolic link  

Switch directories

>>> os.chdir(path)# Switch directories  

File size (bytes)

>>> os.path.getsize(filename) #Return file size, invalid for directory  
65525  

Split the specified path: os Path Split (path) (must have a parameter) returns a tuple containing two elements:
[0]: the path where the file or directory is located, or through os Path Dirname (path) get
[1] : directory name or file name

>>> os.path.split('/home/g004440**/script')  
('/home/g004440**', 'script')  
>>> os.path.split('/home/g004440**/script/') # Include '/' at the end, and return all to the path  
('/home/g004440**/script', '') # Whether script is a directory or not  
>>>  
>>> os.path.split('/home')  
('/', 'home')  
>>> os.path.split('/home/')  
('/home', '')  

Split the file extension and return a tuple

>>> os.path.splitext('/home/g004440**/.vim.rc')  
('/home/g004440**/.vim', '.rc')  

Merge paths. If Linux will use / merge, in Win, yes\

>>> os.path.join('/home', 'g00444054', 'script') # Merge path,  
'/home/g004440**/script'  
>>> os.path.join('/home', 'g00444054', 'script', '')  
'/home/g004440**/script/' # If there is an empty element at the end, an extra '/' will be returned  
>>> os.path.join('', '/home', '', 'g00444054', 'script')  
'/home/g004440**/script' # Empty elements in the front or middle do not affect the result  

Get the absolute path of the path os Path Abspath (path) or os Path Realpath (path)

>>> os.path.abspath('.')  
'/home/g004440**/script'  
>>> os.path.abspath('..')  
'/home/g004440**'  
>>> os.path.abspath('../xxx') # xxx may not exist.  
'/home/g004440**/xxx'  
>>> os.path.realpath('../xxx') # xxx may not exist.  
'/home/g004440**/xxx'  

8. traverse directory

Directory structure test_walk

test_walk/  
    |-- dir0  
    |-- dir1  
    |    |-- dir1.file0  
    |    |-- dir1.file1  
    |-- dir2  
    |    |-- dir2.dir0  
    |    |-- dir2.dir1  
    |-- file0  
    `-- file1  

code

for _s_one_path, _list_dirs, _list_files in os.walk('test_walk'):  
    print(f'{_s_one_path}/')         #Test_ All levels in the walk directory
  
    for _s_one_dir in _list_dirs:    #Current_ S_ One_ dir in a hierarchy in the path directory
        print(f'    {_s_one_dir}/')  
  
    for _s_one_file in _list_files:  #Current_ S_ One_ file in a hierarchy in the path directory
        print(f'    {_s_one_file}')  
  
    print('')  

Output:

test_walk/  
    dir0/   # Test_ Three dirs in the walk directory  
    dir1/  
    dir2/  
    file0   # Test_ Two file s in the walk directory  
    file1  
  
test_walk/dir0/ # Test_ There are no dir and file in the walk/dir0 directory  
     
test_walk/dir1  # Test_ There is no dir in the walk/dir0 directory, there are 2 file s  
    dir1.file0  
    dir1.file1  
     
test_walk/dir2  # Test_ There are two dirs in the walk/dir2 directory, but no file  
    dir2.dir0/  
    dir2.dir1/  
     
test_walk/dir2/dir2.dir0  # Test_ Walk/dir2/dir2 No dir and file in dir0 directory  
     
test_walk/dir2/dir2.dir1  # Test_ Walk/dir2/dir2 No dir and file in dir1 directory  

Posted by kronikel on Thu, 02 Jun 2022 02:57:50 +0530