Work path:
import os # os is the operating system and do interact, send instructions to the operator
print (os.getcwd ()) # get the path of the current working document ***
os.chdir ( "D: \ Python_s25 \ day16") # path switching **
print(os.getcwd())
print (os.curdir)
print (os.pardir)
folder***
os.mkdir ( "a2") # Create Folder
os.rmdir ( "a2") # delete folders
os.makedirs ( 'a1 / a2 / a3') # create folders recursively
os.removedirs ( "a1 / a2 / a3") # delete folders recursively
print (os.listdir (r "D: \ Python_s25 \ day17")) # View all the contents of the current file
file***
os.remove (r "D: \ Python_s25 \ day17 \ a") # delete files completely remove regained
os.rename () # rename
path
print (os.path.abspath ( "test")) # returns the absolute path ***
print (os.path.split (r "D: \ Python_s25 \ day17 \ test")) # path into a path and a file name **
print (os.path.dirname (r "D: \ Python_s25 \ day17 \ test")) # get to the parent directory ***
print (os.path.basename (r "D: \ Python_s25 \ day17 \ test")) # get file name **
print (os.path.join ( "D: \ Python", "day17", "test")) # splicing path *** (very important)
Judge
print (os.path.exists (r "D: \ Python_s25 \ day17 \ blog")) # determine whether there is a current Road King **
print (os.path.isabs (r "D: \ Python_s26 \ day17 \ blog")) # judge is not an absolute path **
print (os.path.isdir (r "D: \ Python_s25 \ day17 \ blog")) # judge is not a folder **
print (os.path.isfile (r "D: \ Python_s25 \ day17 \ blog")) # judge is not a file **
print (os.path.getsize (r "D: \ Python_s25 \ day17 \ 01 today content .py")) # Get File Size
print (os.path.getsize (r "D: \ Python_s25")) # Get File Size ***
Folder Related
os.makedirs ( 'dirname1 / dirname2') may generate a multilayer recursive directory
os.removedirs ( 'dirname1') if the directory is empty, delete, and recursively to the parent directory, should also null, the deleted and so on
os.mkdir ( 'dirname') to generate single-level directory; the equivalent of a shell dirname mkdir
os.rmdir ( 'dirname') delete single-stage empty directory, if the directory is not empty can not be deleted, error; equivalent the shell dirname rmdir
os.listdir ( 'dirname') lists all the files and subdirectories in the specified directory, including hidden files, and a list of ways to print
files related to
os.remove () to delete a file
os.rename ( "oldname", "newname") to rename a file / directory
os.stat ( 'path / filename') get the file / directory information
path related
os.path.abspath (path) returns the absolute path to the path of standardized
os.path.split (path) the path into directory and file name tuple returned
os.path.dirname (path) returns the path of the directory. In fact, os.path.split (path) of the first element
os.path.basename (path) Returns the last path of the file name. How path to / \ or end, it will return a null value, that is the second element os.path.split (path) of.
os.path.exists (path) path, if present, returns True; if the path does not exist, returns False
os.path.isabs (path) if the path is an absolute path, returns True
The os.path.isfile (path) if a path is existing file and returns True. Otherwise it returns False
os.path.isdir (path) if the path is a directory exists, then return True. Otherwise it returns False
os.path.join (path1 [, path2 [, ...]]) will return after a combination of multiple paths parameters before the first absolute path will be ignored
os.path.getatime (path) return path points to the last access time of a file or directory
os.path.getmtime (path) returns the file or directory path points to the last modification time
os.path.getsize (path) returns the size of the path of the *