本文目錄一覽:
- 1、Python的shutil模塊中文件的複製操作
- 2、R、python的文件夾操作
- 3、python 怎麼將輸入目錄內的文件拷貝至另一個目錄的同名文件夾?
- 4、用python如何將文件夾內部分指定文件名的文件複製到目標文件夾,大佬求教!
Python的shutil模塊中文件的複製操作
shutil.copyfile(src, dst):將名為src的文件的內容複製到名為dst的文件中 。
src, dst是文件名
shutil.copy(source, destination)
shutil.copy() 函數實現文件複製功能,將 source 文件複製到 destination 文件夾中,兩個參數都是字元串格式。如果 destination 是一個文件名稱,那麼它會被用來當作複製後的文件名稱,即等於 複製 + 重命名。
source一定是文件名,destination可以是文件名也可以是文件夾名
舉例如下:
shutil 模塊
R、python的文件夾操作
Python OS模塊
1.重命名:os.rename(old, new)
2.刪除:os.remove(file)
3.列出目錄下的 文件 :os.listdir(path)
4.獲取當前工作目錄:os.getcwd()
5.改變工作目錄:os.chdir(newdir)
6.創建多級目錄:os.makedirs(r”c:/python /test”)
7.創建單個目錄:os.mkdir(“test”)
8.刪除多個目錄:os.removedirs(r”c:/python”) #刪除所給路徑最後一個目錄下所有空目錄。
9.刪除單個目錄:os.rmdir(“test”)
10.獲取文件屬性:os.stat(file)
11.修改文件許可權與時間戳:os.chmod(file)
12.執行操作系統 命令:os.system(“dir”)
13.啟動新進程:os.exec(), os.execvp()
14.在後台執行程序:osspawnv()
15.終止當前進程:os.exit(), os._exit()
16.分離文件名:os.path.split(r”c:/python/ hello.py “) – (“c://python”, ” hello.py “)
17.分離擴展名:os.path.splitext(r”c:/python/ hello.py “) – (“c://python//hello”, “.py”)
18.獲取路徑名:os.path.dirname(r”c:/python/ hello.py “) – “c://python”
19.獲取文件名:os.path.basename(r”r:/python/hello.py”) – “hello.py”
20.判斷文件是否存在:os.path.exists(r”c:/python/hello.py”) – True
21.判斷是否是絕對路徑:os.path.isabs(r”./python/”) – False
22.判斷是否是目錄:os.path.isdir(r”c:/python”) – True
23.判斷是否是文件:os.path.isfile(r”c:/python/hello.py”) – True
24.判斷是否是鏈接文件:os.path.islink(r”c:/python/hello.py”) – False
25.獲取文件大小:os.path.getsize(filename)
26.*******:os.ismount(“c://”) – True
27.搜索目錄下的所有文件:os.path.walk()
[2.shutil]
1.複製單個文件:shultil.copy(oldfile, newfle)
2.複製整個目錄樹:shultil.copytree(r”./setup”, r”./backup”)
3.刪除整個目錄樹:shultil.rmtree(r”./backup”)
[3.tempfile]
1.創建一個唯一的臨時文件:tempfile.mktemp() – filename
2.打開臨時文件:tempfile.TemporaryFile()
[4.StringIO] #cStringIO是StringIO模塊的快速實現模塊
1.創建內存 文件並寫入初始數據 :f = StringIO.StringIO(“Hello world!”)
2.讀入內存文件數據:print f.read() #或print f.getvalue() – Hello world!
3.想內存文件寫入數據:f.write(“Good day!”)
4.關閉內存文件:f.close()
rm(list=ls())
path = ‘J:/lab/EX29 –在R語言中進行文件(夾)操作’
setwd(path)
cat(“file A\n”, file=”A”) #創建一個文件A,文件內容是’file A’,’\n’表示換行,這是一個很好的習慣
cat(“file B\n”, file=”B”) #創建一個文件B
file.append(“A”, “B”) #將文件B的內容附到A內容的後面,注意沒有空行
file.create(“A”) #創建一個文件A, 注意會覆蓋原來的文件
file.append(“A”, rep(“B”, 10)) #將文件B的內容複製10便,並先後附到文件A內容後
file.show(“A”) #新開工作窗口顯示文件A的內容
file.copy(“A”, “C”) #複製文件A保存為C文件,同一個文件夾
dir.create(“tmp”) #創建名為tmp的文件夾
file.copy(c(“A”, “B”), “tmp”) #將文件夾拷貝到tmp文件夾中
list.files(“tmp”) #查看文件夾tmp中的文件名
unlink(“tmp”, recursive=F) #如果文件夾tmp為空,刪除文件夾tmp
unlink(“tmp”, recursive=TRUE) #刪除文件夾tmp,如果其中有文件一併刪除
file.remove(“A”, “B”, “C”) #移除三個文件
python 怎麼將輸入目錄內的文件拷貝至另一個目錄的同名文件夾?
這是最近寫的一個類似代碼,你拿去改改
import shutil
import os
import logging
import sys
logger = logging.getLogger(__name__)
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
def cp_or_mv2(src_file, des_dir, is_copy):
print(src_file, des_dir)
if os.path.isfile(src_file):
logger.info(f’from file {src_file}’)
if is_copy:
shutil.copy2(src_file, des_dir)
logger.info(f’copy to {des_dir}’)
else:
des_file = os.path.join(des_dir, src_file)
shutil.move(src_file, des_file)
logger.info(f’move to {des_file}’)
else:
logger.info(f’from dir {src_file}’)
des_dir_level1 = os.path.join(des_dir, src_file)
shutil.copytree(src_file, des_dir_level1, dirs_exist_ok=True)
logger.info(f’to {des_dir_level1}’)
if not is_copy:
shutil.rmtree(src_file)
logger.info(f’deleted {src_file}’)
def process_files_in_txt(txt_file, src_dir, des_dir, is_copy=True):
os.chdir(src_dir)
with open(txt_file, ‘r’, encoding=’utf8′, errors=’ignore’) as f:
for line in f.readlines():
src_file = line.strip()
# logger.info(src_file)
if os.path.exists(src_file):
cp_or_mv2(src_file, des_dir, is_copy)
else:
logger.warning(f'{src_file} missing!’)
if __name__ == ‘__main__’:
process_files_in_txt(r”D:\D\需要拷貝.txt”, # 哪些文件(夾)
r”D:\D\Desktop”, # 從哪個文件夾
r”D:\D\新建文件夾”, # 到哪個文件夾
is_copy=False) # True複製,False剪切
用python如何將文件夾內部分指定文件名的文件複製到目標文件夾,大佬求教!
import glob
import shutil
def copy_file(names,old_name,new_name):
for name in names:
filename = name.split(“\\”)[-1]
#filename:從路徑中截取文件名
shutil.copyfile(old_name + filename, new_name + filename)
files = glob.glob(r’D:/A/1*.txt’)
#files : 搜索得到的符合條件(帶有1開頭的txt)的文件列表
old_path = r’D:/A/’
new_path = r’D:/B/’
copy_file(files,old_path,new_path)
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/153803.html