本文目錄一覽:
python 怎麼把文件夾下所有文件複製
#!/usr/bin/env python
# encoding: utf-8
import os
os.popen(“cp /dd/* /tmp”).read() ####將/dd 文件夾下的所有東西複製到/tmp
Python編程怎麼複製?
解釋器是互動式的,類似於很早的basic,不太適用直接粘貼。所以建議你:
在windows之下安裝的python有一個idle集成開發環境,在那裡new個新文件,複製粘貼源代碼,點run菜單運行即可。也可在windows的「命令提示符」下用python直接執行你用記事本寫好的.py文件。像我們一般都用比較專業的開發環境,比如vs裡面包含的python,總之有很多種選擇,就看自己的喜好了
python批量複製並重命名文件
#! /usr/bin/env python
# coding=utf-8
import os
import shutil
import time
import sys
reload(sys)
sys.setdefaultencoding(‘utf-8’)
def copy_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.copyfile(oldname, newname_1)
shutil.copyfile(oldname, newname_2)
shutil.copyfile(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/”
copy_and_rename(fpath_input, fpath_output)
t2 = time.time() * 1000
print(‘take time:’ + str(t2 – t1) + ‘ms’)
print(‘end.’)
python把一個文件夾下的所有東西複製到另一個文件夾下
from shutil import copy
import os
import re
dest_dir = raw_input(‘Please enter destination path:(split path with “/”)’)
source_dir = raw_input(‘Please enter source path:(split path with “/”)’)
if not dest_dir.endswith(‘/’):
dest_dir += ‘/’
if not source_dir.endswith(‘/’):
source_dir += ‘/’
if os.path.isdir(dest_dir) and os.path.isdir(source_dir):
for root, dirs, files in os.walk(source_dir):
for i in xrange (0, files.__len__()):
sf = os.path.join(root, files[i])
dst = re.sub(‘([A-Za-z]:/.*?)/’, dest_dir, root)
if not os.path.exists(dst):
os.makedirs(dst)
copy(sf, dst)
print ‘Done!’
else:
raise Exception(‘Wrong path entered!’)
raw_input()
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/308621.html