In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "what is the method of python file and path management", so the editor summarizes the following contents, detailed contents, clear steps, and certain reference value. I hope you can get something after reading this article. Let's take a look at this "what is the method of python file and path management" article.
The OS module of the Python standard library encapsulates the API of the operating system and uses a unified API to access the same functions of different operating systems. The OS module contains functions that interact with the operating system's system environment, file system, user database, and permissions. Cross-platform programs can be written by making full use of OS module.
Path = os.getcwd () print (path) print (os.listdir ('.)) # displays the files and folders in the current directory
The os.path module is used to manage files and paths, and obviously it contains a lot of functions for splitting paths. The functions related to the split path in the os.path module are:
1. Split: returns a tuple containing the path and file name of the file
2. Dirname: return the path of the file
3. Basename: returns the file name of the file
4. Splitext: returns a tuple that removes the file extension and the extension.
Import ospath = os.getcwd () print (path) print (os.listdir ('.)) # shows the files and folders in the current directory path = r "d:\ test\ log.log" print (os.path.split (path)) # divided into path and file name print (os.path.dirname (path)) # return file path print (os.path.basename (path)) # return file name print (os.path.splitext (path)) # return path file name and extension
The os.path module also contains functions to build the path. The most commonly used are the expanduser, abspath, and join functions:
1. Expanduser: expand the user's HOME directory
2. Abspath: the absolute path to get the file or path
3. Join: different path separators are used to assemble paths according to different operating system platforms.
Print (os.path.expanduser ('~')) print (os.path.abspath ('.') print (os.path.join ('test','a.py'))
The os.path module also contains several functions to obtain file attributes, including file creation time, modification time, file size, and so on:
1. Getatime: get the access time of the file
2. Getmtime: get the modification time of the file
3. Getctime: get the creation time of the file
4. Getsize: get the file size.
The os module also contains operation functions for files and directories, including creating directories, deleting directories, deleting files, renaming files, etc.
1. Remove: delete the file pointed to by the path path
2. Rmdir: delete the folder pointed to by the path path. The folder must be empty, otherwise an error will be reported.
3. Mkdir: create a folder
4. Rename: rename a file or folder.
The os module also contains functions to modify and judge file permissions, namely chmod and access.
Chmod is used to modify the permissions of the file, and access is used to determine whether the file has the corresponding permissions.
The os module also provides three constants to represent read, write, and executable permissions, namely R_OK, W_OK, and X_OK.
Import os,sys,statfilename = 'log.log'if not os.path.isfile (filename): print ("file not exists") elif not os.access (filename,os.R_OK): os.chmod (filename, stat.S_IRUSR) else: print ("access yes") the above is the content of this article on "what are the methods of python file and path management". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about it, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.