当前位置:网站首页>Rename and delete files

Rename and delete files

2022-06-24 20:48:00 m0_ sixty-two million four hundred and ninety-six thousand nine

Python Of os Module provides methods to help you perform file processing operations , Such as renaming and deleting files

To use this module , You must import it first , Then you can call various related functions

Rename() Method :

Rename() Method requires two parameters , Current file name and new file name

grammar :

os.rename( route \ Old file name , route \ New file name )

If you do not give the new file name path , After renaming, the file will be placed in the same path as the code file

Example :

The following example will rename an existing file foo.txt

Import os

# Rename file foo.txt To fo.txt

Os.rename(foo.txt,fo.txt)

Remove() Method

You can use it. remove() Method to delete the file , You need to provide the file name to be deleted as a parameter

grammar :

Os.remove(file_name)

Python modular , It's a python file , With .py ending , Contains python Object definition and python sentence , When you need other code file methods , No need to rewrite the code , You can directly import the code file into the current file , You can use this method directly

When you need to use methods under other modules , You need to import this module into the current module

from.....import......

Python Of from Statement allows you to import a specified part from a module into the current namespace

Is the name of the method that can be used directly

import modular

Import xx Module to current page , When using the method, you need to add the module name

example :import Module name

When using : Module name . Release method (). Call in this format

Time Time module ,sleep() Script pause :

After import , You can use sleep function , Give function pause time . example :sleep(10)

From  time import sleep

原网站

版权声明
本文为[m0_ sixty-two million four hundred and ninety-six thousand nine]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211325594406.html