
正文
python函数复制文件 python如何复制粘贴文件
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
python 复制文件
用Python把某一目录下的文件复制到指定目录中,代码如下:
1、首先插入必要的库:
import os
import os.path
import shutil
import time, datetime
2、实现复制文件代码如下:
def copyFiles(sourceDir,targetDir):
if sourceDir.find(".svn") 0:
return
for file in os.listdir(sourceDir):
sourceFile = os.path.join(sourceDir,file)
targetFile = os.path.join(targetDir,file)
if os.path.isfile(sourceFile):
if not os.path.exists(targetDir):
os.makedirs(targetDir)
if not os.path.exists(targetFile) or(os.path.exists(targetFile) and (os.path.getsize(targetFile) != os.path.getsize(sourceFile))):
open(targetFile,"wb").write(open(sourceFile,"rb").read())
if os.path.isdir(sourceFile):
First_Directory = False
copyFiles(sourceFile, targetFile)
3、主函数的实现:
if __name__ =="__main__":
print "Start(S) or Quilt(Q) \n"
flag = True
while (flag):
answer = raw_input()
if 'Q' == answer:
flag = False
elif 'S'== answer :
formatTime = getCurTime()
targetFoldername = "Build " + formatTime + "-01"
Target_File_Path += targetFoldername
copyFiles(Debug_File_Path,Target_File_Path)
removeFileInFirstDir(Target_File_Path)
coverFiles(Release_File_Path,Target_File_Path)
moveFileto(Firebird_File_Path,Target_File_Path)
moveFileto(AssistantGui_File_Path,Target_File_Path)
writeVersionInfo(Target_File_Path+"\\ReadMe.txt")
print "all sucess"
else:
print "not the correct command"
相关问答
Q1: python 中如何实现对文件的复制、粘贴
file类中没有提供专门的文件复制函数,因此只能通过使用文件的读写函数来实现文件的复制。这里仅仅给出范例:
src = file("myfile.txt", "w+")
temp = ["hello world! \n"]
src.writelines(temp)
src.close()
src = file("myfile.txt", "r+")
des = file("myfile2.txt", "w+")
des.writelines(src.read()())
src.close()
des.close()
shutil模块是另一个文件,目录的管理接口,提供了一些用于复制文件,目录的函数。copyfile()函数可以实现文件的拷贝,声明如下:
copyfile(src, des)
文件的剪切可以使用move()函数模拟,声明如下:
move(src,des)
功能:移动一个文件或者目录到指定的位置,并且可以根据参数des重命名移动后的文件。
Q2: python IDEL复制上一行的快捷键是什么
1、新建一个python脚本文件file.py(名字任意)。
2、在python脚本中用2个open函数即可实现对该图片的复制,具体实现代码如图。
3、执行上述脚本文件后查看结果,可以看到该图片已被成功复制。
4、对于其他文件例如,文本文件操作方式和上面一样,下图是复制11.txt成功的实际例子。以上文件操作都需要注意文件的路径,路径可以是绝对路径类似"F:\test418\examples\11.txt",也可以是相对路径类似"11.txt"
Q3: 说说在 Python 中如何复制、移动、改名以及删除文件或文件夹
要实现复制、移动、改名以及删除文件或文件夹python函数复制文件,需要用到 shutil 模块,shutil 是 shell util 的简写形式,表示 shell 工具。
调用 shutil.copy(source, destination) 来实现复制文件或文件夹功能,依据 destination 进行区分:
运行结果:
注意: 指定复制的文件夹必须存在,否则会抛出 FileNotFoundError。
shutil 的 copytree(source, destination) 方法会复制整个文件夹,包括它所包含的所有文件夹和文件。source
指定源文件夹,destination 指定新的文件夹。source 和 destination 入参都是字符串。该函数会返回新文件夹的路径。destination 如果不存在,会自动创建。请看下例:
运行结果:
shutil.move(source, destination) 方法会将路径 source 处的文件移动到路径 destination,并返回新位置的绝对路径的字符串。
如果 destination 指向一个文件夹, source 处的文件将移动到 destination 中, 并保持原来的文件名。
运行结果:
注意:
os 模块中的函数,可以实现删除一个文件或一个空文件夹。而 shutil 更强大,使用它可以删除一个非空文件夹!
注意: 因为是永久删除,所以使用这些函数一定要小心!建议调试程序时, 先注释掉这些删除方法,
然后加上 print(), 把要被删除的文件打印出来,确认后,再执行。
打印出来的文件列表确认无误后,再执行 os.unlink(filename) 执行删除操作。
send2trash 模块会将文件夹或文件发送到计算机的回收站。首先,安装它:
安装成功后,调用 send2trash.send2trash 方法,就可以把文件夹或文件发送到计算机的回收站。请看下例:
建议使用 send2trash.send2trash() 函数来删除文件或文件夹,因为以后还可以从回收站还原。但这样做,不
会释放磁盘空间。如果python函数复制文件我们还是希望程序释放磁盘空间, 就要用 os 和 shutil 来删除文件和
文件夹(记得使用之前提出的 print 技巧)。还有一点需要注意, send2trash() 函数只能将文件送到回收站, 但不能从回收站中恢复文件。
Q4: Python的shutil模块中文件的复制操作
shutil.copyfile(src, dst):将名为src的文件的内容复制到名为dst的文件中 。
src, dst是文件名
shutil.copy(source, destination)
shutil.copy() 函数实现文件复制功能,将 source 文件复制到 destination 文件夹中,两个参数都是字符串格式。如果 destination 是一个文件名称,那么它会被用来当作复制后的文件名称,即等于 复制 + 重命名。
source一定是文件名,destination可以是文件名也可以是文件夹名
举例如下:
shutil 模块
关于python函数复制文件和python如何复制粘贴文件的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







