1.os.path.exists(path)
Determine whether the file path path exists, there is a return Ture, there is no return False.
Example:
SYS Import, the shutil, os # pick second parameter path = the sys.argv [. 1] # to make the os path determination module, returns False or True In Flag = os.path.exists (path) IF In Flag: # use shutil module deletes files shutil.rmtree (path) the else: Print ( 'does not exist')
2.os.path.abspath()
Returns the absolute path of a file.
Example:
import os path = os.path.abspath('day04_task.py') print(path)
3.os.stat(path).size
Returns the size of the file to bit units.
OS Import # 1. read the file size (bytes) FILE_SIZE the os.stat = ( '20190409_192149.mp4'). the st_size # 2. little by little to read the file read_size = 0 with Open ( '20190409_192149.mp4', = MODE 'RB') AS F1, Open ( 'a.mp4', MODE = 'WB') AS f2: the while read_size <FILE_SIZE: the chunk = f1.read (1024) # of bytes 1024 to read the maximum f2 .write (the chunk) read_size + = len (the chunk) Val = int (read_size / FILE_SIZE * 100) Print ( '% S %% \ R & lt'% Val, End = '')
4.os.path.join
Stitching path, stitching together the two paths, and the different splicing can be returned depending on the operating system.
import os
Add r # path before, so that failure of the escape character
path = r'd:\test'
path = os.path.join(path,'1.txt')
print(path)
5.os.listdir, to view a directory of all the files [first floor]
import os result = os.listdir(r'D:\code\s21day14') for path in result: print(path)
6.os.walk, to view a directory of all the files [all levels]
os Import the Result = os.walk (r'D: \ code \ s21day14 ') for A, b, c in the Result: # A, are viewing the directory b, files in this directory folder c, files in this directory for C in Item: path the os.path.join = (A, Item) Print (path)