code example:
1 #change the current directory operation2 ImportOS3 4Cur =Os.curdir5 Print("1. Current directory relative path:", cur)6Par =Os.pardir7 Print("2. Parent directory relative path:", par)8CWD =OS.GETCWD ()9 Print("3, the current directory absolute path:", CWD)TenOs.chdir (Os.pardir)#Change the current path OneCWD =OS.GETCWD () A Print("4. Switch to parent directory:", CWD) - -File_str ="' the forFinchOs.listdir (CWD): - " "traverse the parent directory file" " -File_str = f +','+File_str - Print("5. File under parent directory:"+ file_str)
Operation Result:
1. Current directory relative path:. 2, parent directory relative path:. 3, the current directory absolute path: D:\pywork\pyworkspace\temp4, switch to the parent directory after the absolute path: D:\pywork\pyworkspace5, the parent directory file: __pycache__, Yzwx_selenium, Work_dir, temp, Study_demo2, Study_demo,. Idea,
Example of traversing a subdirectory code:
1 "' 2 lists = Os.listdir (CWD)3 for in lists:4 " ' traverse subdirectory ' '5 if os.path.isdir (f):6 Os.chdir (f)7 Print (OS.GETCWD ()) 8 Os.chdir (Os.pardir)
Operation Result:
D:\pywork\pyworkspace\.idead:\pywork\pyworkspace\study_demod:\pywork\pyworkspace\study_demo2d:\pywork\ pyworkspace\tempd:\pywork\pyworkspace\work_dird:\pywork\pyworkspace\yzwx_seleniumd:\pywork\pyworkspace\__ pycache__
Python's File directory operation