Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to understand python os and os.path module

Shulou Source: shulou.com Published: 2022-06-01 03:11:10 09月22日 Update

In this issue, Xiaobian will bring you about how to understand python os and os.path modules. The article is rich in content and analyzed and described from a professional perspective. After reading this article, I hope you can gain something.

python27

OS module

① Directory related

os.getcwd(): Get the current working directory

os.listdir ('/tmp'): Get files and directories in/tmp directory, list type

os.chdir ('/tmp'): Switch working directory to/tmp

os.mkdir ('/tmp'): Create/tmp directory

os.makedirs ('/a/b/c'): Create/a/b/c directory

os.rmdir ('/tmp'): Delete/tmp directory

os.removedirs ('/tmp'): recursively delete empty directories under/tmp

② Document related

os.remove ('a.txt '): remove a.txt

os.rename ('a.txt ','b.txt'): rename a.txt to b.txt

③ User related

os.getuid()

os.getgid()

os.getlogin()

os.path module (processing of paths)

os.path.abspath('. '): Gets the absolute path to the current file or directory, string type

os.path.basename ('/tmp/a.txt '): Returns a.txt string type

os.path.dirname ('/tmp/a.txt '): Returns/tmp string type

os.path.exists ('/tmp'): determines whether a file or directory exists

os.path.split ('/tmp/a.txt '): Returns a tuple containing dirname and basename, this example returns ('/tmp','a.txt' )

os.path. split text ('/tmp/a.txt '): Returns a tuple containing the file name and file extension, this example returns ('/tmp/a','.txt' )

os.path.join ('/tmp','a.txt'): join directories and files or join directories and directories, this example returns '/tmp/a.txt'

os.path.isabs(xxx): Determine if xxx is an absolute path

os.path.isdir(xxx): Determine if xxx is a directory

os.path.isfile(xxx): Determine if xxx is a file

os.path.islink(xxx): Determine if xxx is a link

os.path. samplefile (a,b): Determine whether a b is the same file

os.path.normpath ('/tmp/a//b/'): canonical path form, this example returns/tmp/a/b

os.path.walk ('/tmp'): Recursive tree directory

os.path.getatime(): Get file or directory access time

os.path.getmtime(): Get file or directory modify time

os.path.getctime(): Get file or directory change time

os.path.getsize(): Gets the size of a file or directory

ps: os and os.path are extremely frequent modules used when dealing with file system-related tasks, and should be kept in mind that multiple uses

The above is how to understand python os and os.path module shared by Xiaobian for everyone. If you happen to have similar doubts, you may wish to refer to the above analysis for understanding. If you want to know more about it, please pay attention to the industry information channel.

Tags: Directories files modules examples types paths characters strings content recursion analysis processing work frequent professional small and medium tasks rich content size that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Huawei Xiaomi MySQL Redmi