python的shutilmove
Ⅰ python shutil.问题 为什么无法传值
shutil.file( src, dst) 从源src复制到dst中去。当然前提是目标地址是具备可写权限。抛出的异常信息为IOException. 如果当前的dst已存在的话就会被覆盖掉
shutil.move( src, dst) 移动文件或重命名
shutil.mode( src, dst) 只是会复制其权限其他的东西是不会被复制的
shutil.stat( src, dst) 复制权限、最后访问时间、最后修改时间
shutil.( src, dst) 复制一个文件到一个文件或一个目录!
Ⅱ python shutil.move shutil.哪个快
文件的话shutil.move快,目录的话shutil.
由于shutil.move移动目录时也是进行操作,然后再删除
Ⅲ python里面shutil是什么
1.shutil.fileobj(fsrc, fdst[, length])
将文件内容拷贝到另一个文件中
import shutil
shutil.fileobj(open('old.xml','r'), open('new.xml', 'w'))
2.shutil.file(src, dst)
拷贝文件
shutil.file('f1.log', 'f2.log')
3.shutil.mode(src, dst)
仅拷贝权限。内容、组、用户均不变
shutil.mode('f1.log', 'f2.log')
4.shutil.stat(src, dst)
仅拷贝状态的信息,包括:mode bits, atime, mtime, flags
shutil.stat('f1.log', 'f2.log')
5.shutil.(src, dst)
拷贝文件和权限
shutil.('f1.log', 'f2.log')
6.shutil.2(src, dst)
拷贝文件和状态信息
shutil.2('f1.log', 'f2.log')
7.shutil.ignore_patterns(*patterns)
shutil.tree(src, dst, symlinks=False, ignore=None)
递归的去拷贝文件夹
shutil.tree('folder1', 'folder2', ignore=shutil.ignore_patterns('*.pyc', 'tmp*'))
shutil.tree('f1', 'f2', symlinks=True, ignore=shutil.ignore_patterns('*.pyc', 'tmp*'))
8.shutil.rmtree(path[, ignore_errors[, onerror]])
递归的去删除文件
shutil.rmtree('folder1')
9.shutil.move(src, dst)
递归的去移动文件,它类似mv命令,其实就是重命名。
shutil.move('folder1', 'folder3')
10.shutil.make_archive(base_name, format,...)
创建压缩包并返回文件路径,例如:zip、tar
创建压缩包并返回文件路径,例如:zip、tar
base_name: 压缩包的文件名,也可以是压缩包的路径。只是文件名时,则保存至当前目录,否则保存至指定路径,
如:www =>保存至当前路径
如:/Users/wupeiqi/www =>保存至/Users/wupeiqi/format: 压缩包种类,“zip”, “tar”, “bztar”,“gztar”
root_dir: 要压缩的文件夹路径(默认当前目录)
owner: 用户,默认当前用户
group: 组,默认当前组
logger: 用于记录日志,通常是logging.Logger对象
Ⅳ python怎样批量修改文件名
os.rename(a,b)是把a重命名为b(名字带路径)
os.listdir(path)以列表形式获取路径下文件(包括文件夹),os.listdir(“.”)是当前文件夹
然后。。。想怎么重命名就怎么写程序。。。遍历,(检查,)修改。。。
如果待重命名的文件和py程序在一个文件夹会很方便,重命名不需要路径,像os.rename(“1.txt”,“2.txt”)
如果重命名的规则没法表述,那只能自己一个个改了。。。
Ⅳ python 怎么将输入目录内的文件拷贝至另一个目录的同名文件夹
这是最近写的一个类似代码,你拿去改改
import shutil
import os
import logging
import sys
logger = logging.getLogger(__name__)
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
def cp_or_mv2(src_file, des_dir, is_):
print(src_file, des_dir)
if os.path.isfile(src_file):
logger.info(f'from file {src_file}')
if is_:
shutil.2(src_file, des_dir)
logger.info(f' to {des_dir}')
else:
des_file = os.path.join(des_dir, src_file)
shutil.move(src_file, des_file)
logger.info(f'move to {des_file}')
else:
logger.info(f'from dir {src_file}')
des_dir_level1 = os.path.join(des_dir, src_file)
shutil.tree(src_file, des_dir_level1, dirs_exist_ok=True)
logger.info(f'to {des_dir_level1}')
if not is_:
shutil.rmtree(src_file)
logger.info(f'deleted {src_file}')
def process_files_in_txt(txt_file, src_dir, des_dir, is_=True):
os.chdir(src_dir)
with open(txt_file, 'r', encoding='utf8', errors='ignore') as f:
for line in f.readlines():
src_file = line.strip()
# logger.info(src_file)
if os.path.exists(src_file):
cp_or_mv2(src_file, des_dir, is_)
else:
logger.warning(f'{src_file} missing!')
if __name__ == '__main__':
process_files_in_txt(r"D:\D\需要拷贝.txt", # 哪些文件(夹)
r"D:\D\Desktop", # 从哪个文件夹
r"D:\D\新建文件夹", # 到哪个文件夹
is_=False) # True复制,False剪切
Ⅵ python shuutil.move对异常的处理
可以先判断再做处理。用os.path.isfile函数
Ⅶ python shutil模块函数file和的区别
shutil.file( src, dst) 从源src复制到dst中去。当然前提是目标地址是具备可写权限。抛出的异常信息为IOException. 如果当前的dst已存在的话就会被覆盖掉
shutil.move( src, dst) 移动文件或重命名
shutil.mode( src, dst) 只是会复制其权限其他的东西是不会被复制的
shutil.stat( src, dst) 复制权限、最后访问时间、最后修改时间
shutil.( src, dst) 复制一个文件到一个文件或一个目录
shutil.2( src, dst) 在上的基础上再复制文件最后访问时间与修改时间也复制过来了,类似于cp –p的东西
shutil.2( src, dst) 如果两个位置的文件系统是一样的话相当于是rename操作,只是改名;如果是不在相同的文件系统的话就是做move操作
shutil.tree( olddir, newdir, True/Flase)
把olddir拷贝一份newdir,如果第3个参数是True,则复制目录时将保持文件夹下的符号连接,如果第3个参数是False,则将在复制的目录下生成物理副本来替代符号连接
shutil.rmtree( src ) 递归删除一个目录以及目录内的所有内容
Ⅷ python的shutil.move是不是很慢
是会慢很多
shutil.move的代码,如果src是dir,dst存在也是dir,
则先复制再删除src,所以会很慢,操作系统中是直接移动所以快
def move(src, dst):
"""Recursively move a file or directory to another location.
If the destination is on our current filesystem, then simply use
rename. Otherwise, src to the dst and then remove src.
A lot more could be done here... A look at a mv.c shows a lot of
the issues this implementation glosses over.
"""
try:
os.rename(src, dst)
except OSError:
if os.path.isdir(src):
if destinsrc(src, dst):
raise Error, "Cannot move a directory '%s' into itself '%s'."
% (src, dst)
tree(src, dst, symlinks=True)
rmtree(src)
else:
2(src,dst)
os.unlink(src)
Ⅸ Python 中怎样删除zip压缩文件夹中某个文件
用
PyZipFile
方法很简陋。
是把
zip压缩包里面的文件,除了要删除的文件以外,逐个读取出来,保存到新文件中。最后用新文件覆盖旧文件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#
coding=utf-8
import
zipfile
import
your_delet_file="你要删除的文件名"
old_zipfile='archive.zip'
#新文件
new_zipfile='archve_new.zip'
#新文件
zin
=
zipfile.ZipFile
(old_zipfile,
'r')
#读取对象
zout
=
zipfile.ZipFile
(new_zipfile,
'w')
#被写入对象
for
item
in
zin.infolist():
buffer
=
zin.read(item.filename)
if
(item.filename!=
your_delet_file):
#剔除要删除的文件
zout.writestr(item,
buffer)
#把文件写入到新对象中
zout.close()
zin.close()
#用新文件覆盖旧文件
shutil.move(new_zipfile,old_zipfile)