Python's File directory operation

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.