在Python中复制和重命名文件

Using shutil (shell utilities) module, file operation such as copy, rename, move, etc is very handy. To copy and rename, there are two approaches:

使用shutil(shell实用程序)模块 ,文件操作(例如复制,重命名,移动等)非常方便。 要复制和重命名 ,有两种方法:

  1. Move the file with new name

    用新名称移动文件

  2. Copy and rename file using 'OS' module

    使用“ OS”模块复制和重命名文件

1)移动并重命名文件 (1) Move and Rename file)

move function

移动功能

    shutil.move(src, dst, copy_function=copy2)

The above method recursively moves the file from src to dst and returns the destination.

上面的方法将文件从src递归移动到dst并返回目标。

Reminders,

提醒事项

  • If the destination is an existing directory, then the src object is moved inside the given dst.

    如果目标是现有目录,则将src对象移动到给定的dst中 。

  • In case the destination already exists and is not a directory, it will be overwritten using os.rename().

    如果目标已经存在并且不是目录,则将使用os.rename()覆盖它。

  • In case the destination is on the current filesystem, then os.rename() is used. In the case of symlinks, a new symlink pointing to the target of src will be created in or as dst and src will be removed.

    如果目标位于当前文件系统上,则使用os.rename() 。 对于符号链接 ,将在dst中或作为dst创建指向src目标的新符号链接 ,并删除src 。

  • The default copy_function is copy2(). Using copy() as the copy_function allows the move to succeed.

    默认的copy_functioncopy2() 。 使用copy()作为copy_function可以使移动成功。

用于移动和重命名文件的Python代码 (Python code for move and rename file)

List command:

列出命令:

    -bash-4.2$ ls
    python_samples test test.txt test.txt.copy test.txt.copy2 -

More & rename files:

更多和重命名文件:

# Importing the modules
import os
import shutil

# gets the current working dir
src_dir = os.getcwd() 
# defining the dest directory
dest_file = src_dir + "/python_samples/test_renamed_file.txt"

# moving
shutil.move('test.txt',dest_dir)

# listing the files 
print(os.listdir())

os.chdir(dest_dir)

# list of files in dest
print(os.listdir()) 

Output

输出量

'/home/sradhakr/Desktop/my_work/python_samples/ test_renamed_file.txt'
['python_samples', 'test', 'test.txt.copy', 'test.txt.copy2']
['.git', '.gitignore', 'README.md', 'src', ' test_renamed_file.txt']

使用os和shutil模块进行复制和重命名 (Copy and rename using os and shutil module)

In this approach we use the shutil.copy() function to copy the file and os.rename() to rename the file.

在这种方法中,我们使用shutil.copy()函数复制文件,并使用os.rename()重命名文件。

# Importing the modules
import os
import shutil

src_dir = os.getcwd() #get the current working dir
print(src_dir)

# create a dir where we want to copy and rename
dest_dir = os.mkdir('subfolder')
os.listdir()

dest_dir = src_dir+"/subfolder"
src_file = os.path.join(src_dir, 'test.txt.copy2')
shutil.copy(src_file,dest_dir) #copy the file to destination dir

dst_file = os.path.join(dest_dir,'test.txt.copy2')
new_dst_file_name = os.path.join(dest_dir, 'test.txt.copy3')

os.rename(dst_file, new_dst_file_name)#rename
os.chdir(dest_dir)

print(os.listdir())

Output

输出量

/home/user/Desktop/my_work
['python_samples', 'subfolder', 'test', 'test.txt.copy2', 'test.txt.copy_1']
'/home/sradhakr/Desktop/my_work/subfolder/test.txt.copy2'
['test.txt.copy3']

Summary: shutil (shell utilities module ) is a more pythonic way to perform the file or directory copy , move or rename operations.

简介: shutil (shell实用程序模块)是执行文件或目录复制,移动或重命名操作的更Python方式。

Reference: https://docs.python.org/3/faq/windows.html

参考: https : //docs.python.org/3/faq/windows.html

翻译自: https://www.includehelp.com/python/copy-and-rename-files.aspx

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值