python中怎樣將文件拷貝到指定的目錄下

用readline

inputFile = open("inputFile.txt", "r")
print "Name of the input file: ", inputFile.name;

outputFile = open("outputFile.txt", "a");
print "Name of the output file: ", outputFile.name;

allLines = inputFile.readlines();
for eachLine in allLines:
print "current line content: %s" % (eachLine);

#append into output file
outputFile.write(eachLine);

inputFile.close();
outputFile.close();

⑵ 通過python語言實現 從手機A目錄復制文件到電腦B目錄

這我就不知道了

⑶ python 如何復制整個文件夾到另一個目錄下連文件夾也要復制過去

在 liunx下:

>>>importos
>>>os.system("cp-rf/folder/home/user/folder")

⑷ python 怎樣把當前目錄下的一個子文件夾中的文件到當前目錄下

import os, glob, shutil

sub_dir = 'testdir'

for infile in glob.glob( os.path.join(sub_dir, '*.*') ):
print "current file is: " + infile
dirname, filename = os.path.split( infile)
shutil. (infile, filename)

⑸ python 實現一級目錄下的所有文件與文件夾到指定目錄

'''
python3 實現
將a目錄下所有文件和文件夾到b目錄
'''
import os, shutil

#src 原始目錄, des 目標目錄
def sourcecpy(src, des):
src = os.path.normpath(src)
des = os.path.normpath(des)
if not os.path.exists(src) or not os.path.exists(src):
print("文件路徑不存在")
sys.exit(1)
#獲得原始目錄中所有的文件,並拼接每個文件的絕對路徑
os.chdir(src)
src_file = [os.path.join(src, file) for file in os.listdir()]
for source in src_file:
#若是文件
if os.path.isfile(source):
shutil.(source, des) #第一個參數是文件,第二個參數目錄
#若是目錄
if os.path.isdir(source):
p, src_name = os.path.split(source)
des = os.path.join(des, src_name)
shutil.tree(source, des) #第一個參數是目錄,第二個參數也是目錄

⑹ 用python把文件夾下的所有文件包括文件夾裡面的文件都拷貝到同一個目錄下

importos
importshutil

defwenjian(path):
ifos.path.isfile(path):
shutil.(path,'c:\new_dir')
ifos.path.isdir(path):
lists=os.listdir(path)
foriinlists:
wenjian(i)

foriinos.walk('c:\1'):
wenjian(i)

建議你把檢索到的文件都放到一個新的文件夾里,要不然系統在內同一個文件夾里不停的讀取和容寫入可能會陷入死循環以至出錯。

⑺ python怎麼拷貝文件夾下的文件

def upload_file(src_path, dst_path):
# 目標目錄是否存在,不存在則創建
if not os.path.exists(os.path.dirname(dst_path)):
os.makedirs(os.path.dirname(dst_path))
# 本地文件是否存在,存在則移動到目標目錄下
if os.path.exists(src_path):
shutil.move(src_path, dst_path)

⑻ 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 怎麼把文件夾下所有文件復制

import
os
import
os.path
rootdir
=
「d:\data」
#
指明被遍歷的文件夾
for
parent,dirnames,filenames
in
os.walk(rootdir):
#三個參數:分別返回1.父目錄
2.所有文件夾名字(不含路徑)
3.所有文件名字
for
dirname
in
dirnames:
#輸出文件夾信息
print
"parent
is:"
+
parent
print
"dirname
is:"
+
dirname
for
filename
in
filenames:
#輸出文件信息
print
"parent
is:"
+
parent
print
"filename
is:"
+
filename
print
"the
full
name
of
the
file
is:"
+
os.path.join(parent,filename)
#輸出文件路徑信息

⑽ python中怎樣將文件拷貝到指定的目錄下

代碼:

import os
import shutil
from shutil import Error
from shutil import stat
from shutil import 2
src = "" #需要復制的文件目錄
dst = "" #目標目錄
def jiecptree(src, dst, symlinks=False, ignore=None): #聲明函數 ree( 要復制的目錄,目標目錄,復制符號連接內容到新目錄,沒有要忽略文件)
names = os.listdir(src) #獲得要復制目錄的文件名列表,賦給變數 names
if ignore is not None: #如果 ignore 不是None值
ignored_names = ignore(src, names) # src目錄中要忽略文件的名字賦給 ignored_names
else: # 否則
ignored_names = set() #ignore_name 被 不重復空元素集 賦值
if os.path.isdir(dst):
pass
else:
os.makedirs(dst)
# print"dstfirst:"+dst
errors = [] #聲明 errors列
for name in names: #將names里的元素循環復制給name
if name in ignored_names: #如果name在要求被忽略的列里出現
continue #繼續for循環(跳回for,從新循環下個元素)
srcname = os.path.join(src, name) #將路徑名(src)添加到文名(name)之前然後賦值給 srcname
dstname = os.path.join(dst, name) #將路徑名(dst)添加到文名(name)之前然後賦值給 dstcname
from shutil import Error
# print "name:"+name
# print "src:"+src
# print "dst:"+dst
try: #嘗試
if os.path.islink(srcname):
continue
elif os.path.isdir(srcname): #如果srcname路徑是存在
jiecptree(srcname, dstname, symlinks, ignore)
elif os.path.isdir(dstname):
os.remove(dstname)
2(srcname, dstname)
else: # 否則
2(srcname, dstname) # 復制srcname到dstname
# print "srcname:"+srcname
# print "dstname:"+dstname
# XXX What about devices, sockets etc.? #怎樣裝置
except (IOError, os.error), why: #除(IOError[與文件有關的異常],操作系統異常)外,返回原因
errors.append((srcname, dstname, str(why))) # 向errors列里添加,(要復制的目錄,目標目錄,錯誤原因)
# catch the Error from the recursive jiecptree so that we can 從遞歸復制中捕捉這個錯誤,以便於我們能繼續復制其他文件
# continue with other files
except Error, err: #除錯誤外,返回錯誤:
errors.extend(err.args[0]) #擴展 errors 列,添加(err.args[0] 元素)
try: #嘗試
stat(src, dst) # 從src復制許可權位,上次訪問時間,最後修改時間 到 dst,
except WindowsError: # 除 Windows錯誤 外:
# can't file access times on Windows 在Windows上無法復制文件訪問時間
pass # 通過(不作任何處理)
except OSError, why: # 除 操作系統錯誤 外,返回原因:
errors.extend((src, dst, str(why))) #擴展 errors 列,添加(要復制的目錄,目標目錄,錯誤原因)
if errors: # 如果錯誤
raise Error(errors) # 提示錯誤

更多相關內容可參考資料http://www.viiboo.cn