Python create, copy, move, delete and rename files and folders

In this paper we learn to use Python on computer files and folders operations, including create, copy, move, delete and rename files.

os.mkdir () Create Folder

# Introduced os module, the following code has been introduced by default

1
2
3
4
5
import  os
os.mkdir( '新的文件夹' )
'''注意,如果当前已经有这个文件夹了,就会报错
FileExistsError: [WinError 183] 当文件已存在时,无法创建该文件。: '新的文件夹'
所以我们可以在创建文件夹之前先判断是否存在,不存在再新建。'''

 

os.path.exists ( 'folder name') can be determined folder exists

1
2
3
4
if  not  os.path.exists( '新的文件夹' ):
     os.mkdir( '新的文件夹' )
else :
     print ( '该文件夹已存在,无法创建' )

 

os.makedirs () to create multiple folders

Create multiple folders time, when the innermost layer folder exists error

1
os.makedirs( '第一层文件夹/第二层文件夹/第三层文件夹' )

 

Copy, move, rename module need shutil

Copying and moving files (and rename)

Copy the file shutil.copy ()

shutil.copy ( 'file name', 'object folder) to the destination folder below

shutil.copy ( 'file name', 'destination folder / file name of the new') copied to the destination folder, then rename

Move files shutil.move ()

shutil.move ( 'file name', 'object folder) move to the destination folder below

shutil.move ( 'file name', 'destination folder / file name of the new') move to the destination folder, then rename

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 引入shutil模块
import  shutil
# 复制或移动到目的文件夹下面
# 如果只写目标文件夹的名称的话,如果目标文件夹不存在,则会将复制或移动之后的文件重命名为目标文件夹的名称
# demo.txt 复制到 新的文件夹 下面
shutil.copy( 'demo.txt' , '新的文件夹' )
# file1.txt 移动到 新的文件夹 下面
shutil.move( 'file1.txt' , '新的文件夹' )
# 复制或移动到目的文件夹下面之后,重命名
# 如果目标文件夹不存在,则会报错
# demo.txt 复制到 新的文件夹 下面,并重命名为new1.txt
shutil.copy( 'demo.txt' , '新的文件夹/new1.txt' )
# file2.txt 移动到 新的文件夹 下面,并重命名为 new2.txt
shutil.move( 'file2.txt' , '新的文件夹/new2.txt' )

Copying and moving files, if the destination folder does not exist, or to copy and move the file does not exist, it will be an error.

For mobile to copy and move the file exists in the folder is the following, the different processing results. If the same name exists, any file can be successfully copied and overwrite the destination file. While moving files, the file of the same name exists, the error.

1
2
3
4
5
6
7
8
9
# 建议在复制或移动之前做相应判断
if  os.path.exists( '新的文件夹' ):
     shutil.copy( 'demo.txt' , '新的文件夹' )
else :
     print ( '目的文件夹不存在,无法复制' )
if  os.path.exists( '新的文件夹' ):
     shutil.move( 'file1.txt' , '新的文件夹' )
else :
     print ( '目的文件夹不存在,无法移动' )

 

Copying and moving folders (and rename)

When copy and move folders under the folder with all files and folders are moved or copied to the destination path.

Because of the need to fill in after copying and moving files folder names, so you can write the original name, you can also write new folder name.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 复制文件夹 shutil.copytree()
shutil.copytree( '要复制的文件夹' '目的文件夹/要复制的文件夹' ) 复制到目的文件夹下面
shutil.copytree( '要复制的文件夹' '目的文件夹/新文件夹' ) 复制过去并重命名为 新文件夹
# 移动文件夹 shutil.move()
shutil.copytree( '要移动的文件夹' '目的文件夹' ) 移动到目的文件夹下面
shutil.copytree( '要移动的文件夹' '目的文件夹/新文件夹' ) 复制过去并改名为 新文件夹
'''对于目标路径下是否有同名文件夹,shutil.copytree() 和 shutil.move 运行结果不同。
复制文件夹,目标路径下有同名文件夹,会报错。
移动文件夹,目标路径下有同名文件夹,则会移动到同名文件夹的下面,如果同名文件夹下面还有一个同名文件夹,才会报错'''
# 假设 文件夹1 要移动到的 新文件夹/文件夹1 是存在的
shutil.move( '文件夹1' , '新文件夹/文件夹1' )
# 运行之后,文件夹结构会变成:新文件夹/文件夹1/文件夹1
# 如果 新文件夹/文件夹1/文件夹1 也存在,则会报错
# 所以建议用下面的方法判断是否存在同名文件夹
import  os
import  shutil
if  not  os.path.exists( '新的文件夹/文件夹1' ):
     shutil.copytree( '文件夹1' '新的文件夹/文件夹1' )
else :
     print ( '该文件夹已存在,无法复制' )
# 对于移动文件夹来说,则下面两个的执行结果是一样的
if  not  os.path.exists( '新的文件夹/文件夹1' ):
     shutil.move( '文件夹1, ' 新的文件夹')
else :
     print ( '该文件夹已存在,无法移动' )
if  not  os.path.exists( '新的文件夹/文件夹1' ):
     shutil.move( '文件夹1, ' 新的文件夹')
else :
     print ( '该文件夹已存在,无法移动' )

 

重命名 os.rename()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 首先要引入os模块,如果程序前面已经引入了,则不用重复引入
import  os
# demo.txt 重命名为 new.txt
os.rename( 'demo.txt' , 'new.txt' )
# 文件夹1 重命名为 文件夹2
os.rename( '文件夹1' , '文件夹2' )
# 如果目的文件夹不是当前文件夹下面,则相当于移动并重命名,跟shutil.move()执行效果一样
# 所以下面这两个语句运行结果是相同的
# demo.txt 重命名为 new.txt,并移动到 目的文件夹 下面
os.rename( 'demo.txt' , '目的文件夹/new.txt' )
shutil.move( 'demo.txt' , '目的文件夹/new.txt' )
# 文件夹1 移动到 新的文件夹 下面
os.rename( '文件夹1' , '新的文件夹/文件夹1' )
shutil.move( '文件夹1' , '新的文件夹/文件夹1' )
# 同样重命名之前需要判断是否有同名文件和文件夹,这里不再重复了

 

删除

删除文件 os.remove()

1
2
3
4
# 删除 file1.txt
os.remove( 'file1.txt' )
# 删除 文件夹1 下面的 file1.txt
os.remove( '文件夹1/file1.txt' )

 

删除文件夹 shutil.rmtree()

1
2
# 删除 文件夹1
shutil.rmtree( '文件夹1' )

Guess you like

Origin www.cnblogs.com/itzzp/p/12422242.html