Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the file operation modification modules in python

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you what the python file operation modification module, I believe most people do not understand, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

1. Use OS module for path path operation

The syntax for the operation is os.path. Method name ()

Import os # first import the os module

Abspath () method: gets the full path of the current file with an argument of file

File_path = os.path.abspath (_ _ file__) # get the full path print (file_path) of the current file

Dirname () method: gets the full directory path of the current file, with the parameter being the full path of the file

Dir_path = os.path.dirname (file_path) # get the full path print (dir_path) of the current file directory

Join () method: use the stitching method to stitch the path. The first parameter is the file directory path, and the second parameter is the path to be stitched.

Img_path = os.path.join (dir_path, 'img') # splicing path print (img_path)

Basename () method: gets the file name with the parameter of the file's full path

File_name = os.path.basename (file_path) # get the file name print (file_name)

Split () method: cut method, get directory and file name, the result is a tuple

Tuple_path = os.path.split (file_path) # get directory and filename print (tuple_path) 2.pathlib module

To use the pathlib module, you need to import the Path class, instantiate the Path class as a path object, or initialize a new path directly with the Path class

From pathlib import Pathpath = Path () print (path) # shows the current directory path2 = Path ('var',' log', 'message') # path initialization print (path2) path3 = Path (' / var', 'log',' message') # path initialization print (path3)

Joinpath () method: stitching path

Path4 = path3.joinpath ('message.log') # path stitching / var/log/message/message.logprint (path4)

Parent: returns the directory path

Path5 = path4.parent # returns the directory path print (path5)

Parents: returns a path object, which can be obtained through for loop traversal

Path6 = path4.parents # returns an object print (path6)

Name: get the file name

For i in path6: print (I) print (path4.name) # message.log

Suffix: get the extension

Print (path4.suffix) # extension

Suffixes: get a list of extensions, list types

Print (path4.suffixes) # extension list

Cwd () method: gets the current name of the file

Print (path4.cwd ()) # current directory

Glob () method: a wildcard method, such as'* .log', indicates that all files ending in .log are found

Objs = path4.parent.parent.glob ('* .log') # matches the file ending in .log in the directory for i in objs: print (I) 3.sys module

Sys module needs to be imported before use.

Import sys

Sys.argv: to pass parameters from outside the program to the program

Print (sys.argv [0]) # current file path print (sys.argv.__len__ ()-1) # number of parameters print (sys.argv [1]) # first parameter

Sys.path: get the set of strings that specify the search path of the module. You can put the written module under a given path and find it correctly when you import in the program.

Search path for print (sys.path) # Python

Sys.exit (n): exit the program, exit (0) when exiting normally

Import sysprint ("start") try: sys.exit (1) except SystemExit: print ("quit") print ("quit") # output: # start # quit

Sys.modules: returns the module field imported by the system. Key is the module name and value is the module.

4.shutil module

Shutil module needs to be imported before use.

Copy () method: the file system sends a copy of the contents of one file to another, similar to the copy of a Linux system.

Import shutil#. / monitor.py source file shutil.copy ('. / monitor.py', 'hello.py') 5.copy module

Copy module needs to be imported before use.

Copy.copy (): shallow copy, copying only the parent object, not the internal child objects of the object

Copy.deepcopy (): deep copy, copying the object and all children of the object

Import copylst = [1123] lst.copy () # shallow copy string1 = copy.copy ('321') print (string1) # 32 "Deep copy string2 = copy.deepcopy ('321') print (string2) # 3216. The problem of diagonal rod in file path

'/': positive diagonal bar, which is used as delimiter under linux

'\': backslash, which is used as a separator under windows

Note: the background image path of the page must be a'/ 'forward slash, and a'\ 'backslash sometimes represents an escape character.

The above is all the contents of the article "what are the file operation modification modules in python?" Thank you for your reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report