本文目录一览:
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()