python 复制替换文件_在Python中复制和替换文件
python 复制替换文件
The python language provides a built-in module "shutil", which offers numerous high-level operations on files and collections of files. In particular, functions are provided to support the file copying and removal.
python语言提供了一个内置的模块“ shutil” ,该模块对文件和文件集合提供了许多高级操作。 特别是,提供了支持文件复制和删除的功能。
拷贝文件 (Copy file)
shutil.copyfile(src, dst, *, follow_symlinks=True)
Copies the contents of file from source(src) to destination(dst). src and dst are the path names as a string. dst must be the complete target file name.
将文件内容从源( src )复制到目标( dst )。 src和dst是字符串形式的路径名。 dst必须是完整的目标文件名。
Reminders,
提醒事项
If src and dst are the same locations, SameFileError is raised.
如果src和dst位于相同的位置, 则会引发SameFileError 。
The dst must be writable, else an IO Error will be raised.
dst必须可写,否则将引发IO错误 。
If dst already exists, it will be replaced.
如果dst已经存在,它将被替换。
If follow_symlinks are false and src is a symbolic link, then a new symbolic link will be created instead of copying the file src points to.
如果follow_symlinks为false并且src是符号链接,则将创建一个新的符号链接,而不是将src指向的文件复制到该链接。
使用shutil.copyfile()复制文件的Python代码 (Python code to copy the files using shutil.copyfile() )
# importing the modules
import os
import shutil
# getting the current working directory
src_dir = os.getcwd()
# printing current directory
print(src_dir)
# copying the files
shutil.copyfile('test.txt', 'test.txt.copy2') #copy src to dst
# printing the list of new files
print(os.listdir())
Output
输出量
/home/sradhakr/Desktop/my_work
'test.txt.copy2'
['python_samples', 'test.txt', 'test', 'test.txt.copy', 'test.txt.copy2']
移动文件 (Move files)
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_function是copy2() 。 使用copy()作为copy_function可以使移动成功。
使用Python代码移动文件 (Python code to move files)
Commnd to get the list of files and directories:
Commnd获取文件和目录列表:
-bash-4.2$ ls
python_samples test test.txt test.txt.copy test.txt.copy2
Code to move files:
移动文件的代码:
# Importing the modules
import os
import shutil
# getting src & dest directories
src_dir = os.getcwd() #gets the current working dir
dest_dir = src_dir + "/python_samples"
# move method to move the file
shutil.move('test.txt',dest_dir)
# the file 'test.txt' is moved from src to dest
print(os.listdir())
os.chdir(dest_dir)
# list of files in dest
print(os.listdir())
Output
输出量
'/home/sradhakr/Desktop/my_work/python_samples/test.txt'
['python_samples', 'test', 'test.txt.copy', 'test.txt.copy2']
['.git', '.gitignore', 'README.md', 'src', 'test.txt']
Reference: https://docs.python.org/3/faq/windows.html
参考: https : //docs.python.org/3/faq/windows.html
翻译自: https://www.includehelp.com/python/copy-and-replace-files-in-python.aspx
python 复制替换文件
python 复制替换文件_在Python中复制和替换文件相关推荐
- ad19生成gerber文件_在“AD19”中怎样将PCB文件转换为GERBER
四川自贡是历史悠久的老工业城市,上世纪八.九十年代,自贡的锅炉.泵业.阀门全国闻名,在近年发展中,电子产业也取得可喜的成绩.Altium Designer在设计电子产品中是应用较多的工具,它的版本更新 ...
- python代码写入方式_【Python 1-17】Python手把手教程之——文件的读写以及I/O操作...
作者 | 弗拉德 来源 | 弗拉德(公众号:fulade_me) 从文件中读取数据 文本文件可存储的数据量很多,每当需要分析或修改存储在文件中的信息时,读取文件都很有用,对数据分析应用程序来说尤其 如 ...
- python界面显示图片更换背景_用python制作一个简陋的证件照换底色的桌面控制台应用...
获取抠图API密钥 前往RemoveBg官网注册一个账号 注册账户界面已翻译,Api每月可用50次 注册成功后登录,按图示所选点击 点击 Get Api Key 获取Api密钥 安装所需支持库 imp ...
- python 比赛成绩预测_使用Python进行体育竞技分析(预测球队成绩)
今天我们用python进行体育竞技分析,预测球队成绩 一. 体育竞技分析的IPO模式 : 输入I(input):两个球员的能力值,模拟比赛的次数(其中,运动员的能力值,可以通过发球方赢得本回合的概率来 ...
- win7搜索文件怎么搜索文件名中带圆括号的文件?
win7搜索文件怎么搜索文件名中带圆括号的文件? System.FileName:~="(" 这样就行. 括号上加个引号 ~= 是包含的意思, ~< 是以什么为开头, = 是 ...
- python从文件夹中提取指定文件_使用Python实现从各个子文件夹中复制指定文件的方法...
之前用来整理图片的小程序,拿来备忘,算是使用Python复制文件的一个例子. # -*- coding: utf-8 -*- #程序用来拷贝文件并输出图片采集日期等其他信息到Excel中 #文件夹结构 ...
- python生成wps文件_使用Python操作XLS文件(wps中叫et)
一旦TE需要* *信息的列表,我导出一个txt文件与python和扔给他们,但是他们很不开心,哈哈,因为他们想要将数以百计的数据放到xls文件列表输出,工作数量太大,所以我问我出口成xls文件然后给他 ...
- cmd中如何运行python文件_在cmd中运行.py文件: python的操作步骤
在cmd中运行.py文件: python的操作步骤 1 打开cmd, 不改变运行的目录: 输入python 空格 调试好的python文件路径 或者python 空格 将python文件拖入cmd ...
- shell运行python文件_在python shell中运行python文件的实现
在python shell中运行python文件的实现 最近在学习flask开发,写好程序后需要在python shell中运行测试功能.专门抽时间研究了下,总结以防止以后遗忘. 这是测试文件的结构, ...
最新文章
- MySQL zip压缩包安装
- oracle表空间放在别的服务器,OracleXE的APEX可以访问同一服务器上的另一个Oracle数据库(10g),还是受限于XE数据库中的那些用户/表空间?...
- Centos 7 KVM安装win10
- 404未找到是什么意思_http404未找到怎么解决(404未找到与9最常见的HTTP错误解释)...
- C语言将结点s赋给表L,数据结构-单链表
- 操作系统面试题(二)
- lua游戏脚本自动打怪_了解Lua(官方文档翻译)
- 学习数码相框1.2.0.0字符的编码方式_显示点阵文字_freetype_在PC上测试freetype
- Azide连接的不同基团Azide-NHS Ester/BDP FL/diSulfo/Pyrene
- Python学习(七)if语句
- 深度学习 神经网络 神经元 单层神经网络的实现
- 赫夫曼压缩解压(java)
- VALSE2019总结(6)-年度总结-GAN
- 脉冲式激光测距机原理
- oracle通过值查字段,Oracle 中 根据值 查询 所在 表和字段
- 我的苹果手机的越狱之旅
- 为什么我的MATLAB激活成功后打开还是激活界面!
- ACREL-5000能耗监测及ACREL-2000电力监控系统的研究与应用
- “理想”有什么理想?
- 计算机能力培训计划,计算机技能培训计划-20210707025117.pdf-原创力文档