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

How to use os and shutil modules in Python

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

It is believed that many inexperienced people have no idea about how to use os and shutil modules in Python. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Collection of practical methods of os and shutil Modules in Python

Type: reprint time: 2014-05-13

This article mainly introduces the collection of practical methods of os and shutil modules in Python, which can be referenced by friends who need it.

The copy code is as follows:

# os module

Os.sep can replace operating system-specific path delimiters. Under windows, it is'\'

The os.name string indicates the platform you are using. For example, for Windows, it is' nt', but for Linux/Unix users, it is' posix'.

The os.getcwd () function gets the current working directory, that is, the directory path where the current Python script works

Os.getenv () gets an environment variable, if no none is returned

Os.putenv (key, value) sets an environment variable value

Os.listdir (path) returns all file and directory names under the specified directory

The os.remove (path) function is used to delete a file

The os.system (command) function is used to run the shell command

The os.linesep string gives the line Terminator used by the current platform. For example, Windows uses'\ r\ n' while Mac uses'\ r'

The os.path.split (path) function returns the directory name and file name of a path

The os.path.isfile () and os.path.isdir () functions verify whether the given path is a file or a directory, respectively.

The os.path.exists () function is used to verify whether the given path really exists.

Os.curdir returns the current directory ('.')

Os.mkdir (path) creates a directory

Os.makedirs (path) Recursive creation directory

Os.chdir (dirname) change the working directory to dirname

Os.path.getsize (name) gets the file size and returns 0L if name is a directory

Os.path.abspath (name) gets the absolute path

Os.path.normpath (path) specification path string form

Os.path.splitext () separates file name from extension

Os.path.join (path,name) connects a directory to a file name or directory

Os.path.basename (path) returns the file name

Os.path.dirname (path) returns the file path

Os.walk (top,topdown=True,onerror=None) traverses the iterative directory

Os.rename (src, dst) renames file or directory src to dst if dst is an existing directory, OSError. In Unix, if dst is in existence and is a file, if the user has permission, it will be replaced quietly. The operation will fail in some Unix if src and dst are on different file systems. If successful, the naming operation will be an atomic operation (this is required by POSIX). On Windows, if dst already exists, OSError will be thrown, even if it is a file. Valid in unix,Windows.

Os.renames (old, new) recursively renames a folder or file. Like rename ()

# shutil module

Shutil.copyfile (src, dst) is copied from the source src to dst. Of course, the premise is that the destination address is writable. The exception message thrown is IOException. If the current dst already exists, it will be overwritten

Shutil.move (src, dst) move files or rename

Shutil.copymode (src, dst) will only copy its permissions and nothing else will be copied.

Shutil.copystat (src, dst) replication permission, last access time, last modification time

Shutil.copy (src, dst) copy a file to a file or a directory

Shutil.copy2 (src, dst) copy the file on the basis of copy. The last access time and modification time are also copied, something similar to cp-p.

Shutil.copy2 (src, dst) if the file system in two locations is the same, it is equivalent to rename operation, just renaming it; if it is not in the same file system, it is doing move operation.

Shutil.copytree (olddir, newdir, True/Flase)

Copy the olddir to a newdir. If the third parameter is True, the symbolic connection under the folder will be maintained when copying the directory. If the third parameter is False, a physical copy will be generated under the copied directory to replace the symbolic link.

Shutil.rmtree (src) recursively deletes a directory and all its contents

After reading the above, have you mastered how to use the os and shutil modules in Python? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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

Servers

Wechat

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

12
Report