當前位置:首頁 » 編程語言 » python復制文件

python復制文件

發布時間: 2022-01-08 02:42:39

『壹』 python 中如何實現對文件的復制、粘貼

file類中沒有提供專門的文件復制函數,因此只能通過使用文件的讀寫函數來實現文件的復制。這里僅僅給出範例:
src = file("myfile.txt", "w+")
temp = ["hello world! \n"]
src.writelines(temp)
src.close()

src = file("myfile.txt", "r+")
des = file("myfile2.txt", "w+")
des.writelines(src.read())
src.close()
des.close()

shutil模塊是另一個文件,目錄的管理介面,提供了一些用於復制文件,目錄的函數。file()函數可以實現文件的拷貝,聲明如下:
file(src, des)
文件的剪切可以使用move()函數模擬,聲明如下:
move(src,des)
功能:移動一個文件或者目錄到指定的位置,並且可以根據參數des重命名移動後的文件。

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

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 怎麼復制文件夾下部分文件

1、可以配置無密碼訪問或者用sshpass在shell中存密碼
2、實例
ip.txt包含ip列表,每行一個ip
test.sh保護修改配置的命令或者直接修改好,復制到遠程指定路徑。
3、代碼
#!/bin/sh
for
ip
in
`cat
ip.txt`;
do
echo
${ip};
scp
-
P22

test.sh
root...

『伍』 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 復制文件到指定文件夾

復制的話,你可以全選之後的話,直接按一個操作鍵就可以

『柒』 如何用python復制粘貼文件到指定文件夾,windows

可以使用document.getElementsByTagName(\"video\")[0]).src來獲得,
請問python
如何獲得我通過safari
模擬iPhone,返回得HTML文件中沒有看到<video>標簽,
python可以通過類似iOS中的webView
來獲得!

『捌』 請教一個Python任務:復制部分文件並刪除

思路:1.利用os.listdir()獲得mainfolder文件夾中的所有文件夾的名稱
2.建立一個計數count<=0循環,在每個文件夾中先移動後刪除
提示:模塊為os.shutil模塊

『玖』 python把一個文件夾下的所有東西復制到另一個文件夾下

fromshutilimport
importos
importre
dest_dir=raw_input('Pleaseenterdestinationpath:(splitpathwith"/")')
source_dir=raw_input('Pleaseentersourcepath:(splitpathwith"/")')

ifnotdest_dir.endswith('/'):
dest_dir+='/'
ifnotsource_dir.endswith('/'):
source_dir+='/'
ifos.path.isdir(dest_dir)andos.path.isdir(source_dir):
forroot,dirs,filesinos.walk(source_dir):
foriinxrange(0,files.__len__()):
sf=os.path.join(root,files[i])
dst=re.sub('([A-Za-z]:/.*?)/',dest_dir,root)
ifnotos.path.exists(dst):
os.makedirs(dst)
(sf,dst)
print'Done!'

else:
raiseException('Wrongpathentered!')

raw_input()

『拾』 如何使用python代碼,從當前文件夾一個文件里復制字元到另一個文件夾下的同名文件里,文件有多個!

importos
importre
reg=re.compile("指定內容正則表達式")
source="一個文件夾路徑"
target="另一個文件夾路徑"
forfilenameinos.listdir(source):
fullname=os.path.join(source,filename)
targetfile=os.path.join(target,filename)
ifos.path.isfile(fullname)andos.path.splitext(filename)[1].lower()==".txt":
text=reg.search(open(fullname).read()).group(0)
open(targetfile,'w').write(text)

熱點內容
好醫生連鎖店密碼多少 發布:2024-09-20 05:09:38 瀏覽:15
魔獸腳本代理 發布:2024-09-20 05:09:35 瀏覽:98
python登陸網頁 發布:2024-09-20 05:08:39 瀏覽:757
安卓qq飛車如何轉蘋果 發布:2024-09-20 04:54:30 瀏覽:178
存儲過程中in什麼意思 發布:2024-09-20 04:24:20 瀏覽:315
php顯示數據 發布:2024-09-20 03:48:38 瀏覽:501
源碼安裝軟體 發布:2024-09-20 03:44:31 瀏覽:354
入門編程游戲的書 發布:2024-09-20 03:31:26 瀏覽:236
e盒的演算法 發布:2024-09-20 03:30:52 瀏覽:144
win10登錄密碼如何修改登錄密碼 發布:2024-09-20 03:09:43 瀏覽:71