Python OS module important points of knowledge

Source: Internet
Author: User
Tags parent directory

Python OS module important points of knowledge

This is very important, mainly about the file path, I have stepped on a lot of pits, today summed up, convenient to avoid the path associated with the various pits!

1, first we want to get all the folders under a folder and files ( not including files in subfolders)

Lists = Os.listdir (Path)

2, want to get all the folders under a folder (Path) and files (including files within subfolders)

def Listdir (path):      for inch Os.listdir (path):         = os.path.join (path, filename        )if  (os.path.isfile (filename)):            Print  Pathname        Else:            listdir (pathname)

3, get the parent directory folder:

C:\test    getpath.py    \sub         sub_path.py

For example, in the sub_path.py file of this code Os.path.abspath (Os.path.dirname (__file__)) can get the absolute path of the \sub\ directory

For example, in the sub_path.py file of this code Os.path.abspath (os.path.dirname (os.path.dirname (__file__))) You can get the absolute path to the \test\ directory

4, get the current directory and the parent directory

└──folder    ├──data    │   └──data.txt    └──test        └──test.py
Print '* * * Get current directory * * *'PrintOS.GETCWD ()PrintOs.path.abspath (Os.path.dirname (__file__))Print '* * * Get Parent Directory * * *'PrintOs.path.abspath (Os.path.dirname (Os.path.dirname (__file__)))PrintOs.path.abspath (Os.path.dirname (OS.GETCWD ()))PrintOs.path.abspath (Os.path.join (OS.GETCWD (),".."))Print '* * * Get Upper Parent Directory * * *'PrintOs.path.abspath (Os.path.join (OS.GETCWD (),".. /.."))Get current directory ***/workspace/demo/folder/Test/workspace/demo/folder/TestGet Parent Directory ***/workspace/demo/folder/workspace/demo/folder/workspace/demo/folderGet Upper Parent Directory ***/workspace/demo

In addition

# CurDir  indicates that the current folder print(os.curdir)#Pardir  represents the previous folder print (Os.pardir)

5, how to get the path of the called file when calling a file from another directory

C:\test    getpath.py    \sub         sub_path.py

For example, there is a directory named sub under the C:\test directory, C:\test directory under the Getpath.py,sub directory has sub_path.py,getpath.py call sub_path.py; we are in C: \ Test to execute the getpath.py.

If we use sys.path[0 in sub_path.py, we actually get the directory path "C:\test" where the getpath.py is located, because the Python virtual machine is executed from the beginning of the getpath.py.

If you want to get the path to the sub_path.py, you get this:

Os.path.split (Os.path.realpath (__file__)) [0]

Although __file__ is the full path of the. py file, this variable sometimes returns a relative path, sometimes an absolute path, so it is also handled with the Os.path.realpath () function.

In this case, the Os.path.realpath (__file__) output is "C:\test\sub\sub_path.py", while Os.path.split (Os.path.realpath (__file__)) [0] The output is "C:\test\sub".

In short, for example, the difference between OS.GETCWD (), sys.path[0] (sys.argv[0]) and __file__ is this:

Assume that the directory structure is:

C:\test  |  [dir] GetPath    |    [File] path.py    [dir] Sub      |      [File] sub_path.py

Then we execute the python getpath/path.py below the C:\test, at which point the values corresponding to each usage in sub_path.py are actually:

OS.GETCWD () "C:\test", which takes the starting execution directory

Sys.path[0] or sys.argv[0] "C:\test\getpath", which is the directory where the script was executed initially

Os.path.split (Os.path.realpath (__file__)) [0] "C:\test\getpath\sub", which is the directory where __file__ files sub_path.py



Python OS module important points of knowledge

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.