❶ python批量復制並重命名文件
#! /usr/bin/env python
# coding=utf-8
import os
import shutil
import time
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
def _and_rename(fpath_input, fpath_output):
for file in os.listdir(fpath_input):
#if os.path.splitext(file)[1] == ".jpg":
oldname = os.path.join(fpath_input, file)
newname_1 = os.path.join(fpath_output,
os.path.splitext(file)[0] + "_1.jpg")
newname_2 = os.path.join(fpath_output,
os.path.splitext(file)[0] + "_2.jpg")
newname_3 = os.path.join(fpath_output,
os.path.splitext(file)[0] + "_3.jpg")
#os.rename(oldname, newname)
shutil.file(oldname, newname_1)
shutil.file(oldname, newname_2)
shutil.file(oldname, newname_3)
if __name__ == '__main__':
print('start ...')
t1 = time.time() * 1000
#time.sleep(1) #1s
fpath_input = "C:/Users/jack/Desktop/shopimg/0708/"
fpath_output = "C:/Users/jack/Desktop/shopimg/0708/"
_and_rename(fpath_input, fpath_output)
t2 = time.time() * 1000
print('take time:' + str(t2 - t1) + 'ms')
print('end.')
❷ python 復制文件
報錯多半是這句targetDir = targetDir+'/'+os.path.split(sourceDir)[1]
你這句把本來的targetDir覆蓋了,導致後面的文件的目標文件夾被修改
發個我寫的吧,參考下吧
defFile(sourceDir,targetDir):
ifnotos.path.exists(targetDir):
os.makedirs(targetDir)
forfilenameinos.listdir(sourceDir):
path=os.path.join(sourceDir,filename)
ifos.path.isdir(path):
targetSubDir=os.path.join(targetDir,filename)
File(path,targetSubDir)
else:
targetPath=os.path.join(targetDir,filename)
open(targetPath,'wb').write(open(path,'rb').read())
❸ python 怎麼實現兩台伺服器上批量復制文件
1、把excel里文件名那一列復制,粘進一個空白的文本文件,命名為filelist.txt,上傳到伺服器。
2、在伺服器上使用腳本導出,python腳本 fileCp.py 。
代碼示例:
#! python
#coding:utf-8
##!/usr/bin/python
# Filename : fileCp.py
import sys
import os
import shutil
fileList='filelist.txt'
targetDir='files'
filedir = open(fileList)
line = filedir.readline()
log = open('running.log','w')
while line:
line = line.strip('\n');
basename = os.path.basename(line)
exists = os.path.exists(line)
if exists :
print ' '+line+' to '+os.getcwd()+'/'+targetDir+'/'+basename
log.write(' '+line+' to '+os.getcwd()+'/'+targetDir+'/'+basename+'\r\n')
shutil.(line,targetDir+'/'+basename)
else:
print line+' not exists'
log.write(line+' not exists'+'\r\n')
line = filedir.readline()
log.close()
❹ python 中如何實現對文件的復制、粘貼
用shutil模塊
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import os
import os.path
from shutil import
dest_dir = ur'd:\新建文件專夾屬'
if not os.path.isdir(dest_dir):
os.makedirs(dest_dir)
file_path = ur'c:\123\1.txt'
(file_path, dest_dir)
❺ python怎樣壓縮和解壓縮ZIP文件
Python壓縮ZIP文件:
importzipfile
f=zipfile.ZipFile(target,'w',zipfile.ZIP_DEFLATED)
f.write(filename,file_url)
f.close()
其中target:是壓縮後要保存的路徑,可以是: 'C:/temp/'
ZIP_DEFLATED:表示壓縮,還有一個參數:ZIP_STORE:表示只打包,不壓縮。
這個Linux中的gz跟tar格式有點類似.
write方法如果只有一個參數filename的話,表示把你filename所帶的路徑全部壓縮到zip文件中。如果帶兩個參數,表示把filename路徑中的那個file壓縮一下並且存放到file_url中,中間沒有增加任何的文件夾。
如果要壓縮很多的文件,循環的write就ok了, 最後close掉。
Python解壓ZIP文件:
f=zipfile.ZipFile("zipfilePath",'r')
forfileinf.namelist():
f.extract(file,"temp/")
zipfilePath是壓縮文件的路徑
循環訪問該壓縮文件中的文件,並且一個一個file的解壓到對應的"temp"文件夾中
❻ python 中如何壓縮文件,並指定文件的壓縮之後的大小。
這個簡單啊。你先壓縮成一個ZIP文件。比如 example.zip
然後用python將它分割成,5個文件。e1,e2,e3,e4,e5
郵件發出去後,對方收到郵件,另存附件,然後在目錄下運行
e1+e2+e3+e4+e5 example.zip
此時windows就將依次將5個文件復制到同一個文件里去。
❼ python—os模塊批量修改文件名&復制文件
1、修改文粗嘩件名
導入os模塊
這里舉的例子是:對目錄下的所有文件重命名,雹鎮格式為一個字元串+6位遞增數
注意:其中zfill(6)的作用是將1變為6位數,缺少位置0代替,結果就是00001
2、復制文件
注意:還有其它復制方法,可以自行網路~!岩肆行
3.格式化輸出
PS:覺得這篇文章有用的朋友,多多點贊打賞哦~!