簡體   English   中英

使用python將文件從一個目錄轉移到另一個目錄

[英]Transfer files from one directory to another if they match a condition using python

如果文件名符合特定條件,例如,filename1 = extra_filename2,則需要使用python在UNIX中將文件從一個目錄移動/復制到另一個目錄。 我已經准備了以下腳本,但是它給出了錯誤。 有人可以糾正我嗎:

#!/usr/bin/python
import shutil, fnmatch, os

for filename in os.listdir('/home/root/Desktop/'):
        try:
                file1 = fnmatch.fnmatch(filename, '*.txt')
                file2 = fnmatch.fnmatch(filename, '*.txt')

        if file1 = extra_'file2'
                print "True"

shutil.copy2('/home/root/Desktop/file1','/home/root/Desktop/Archive',follow_symlinks=True)
import shutil,glob
for fname in glob.glob("/path/to/some*.txt"):
    shutil.copy("/dest/path",fname)

謝謝大家的答復,我很抱歉無法傳達我的查詢,而我只是提供了原始代碼,這是絕對錯誤的。 基本上我正在尋找類似下面的東西:

導入OS導入Shutil導入重新

來源=(“ sourcePath”)

文件= os.listdir(源)

用於文件中的名稱:fullpath =源+“ /” +名稱Name2 =名稱+“ filename2”#如果文件中有re.match(“ filename regex”,name)和Name2,則打印全路徑:

  #shutil.rmtree("destinationPath"+name) shutil.copytree(fullpath, "destinationpath"+name) shutil.copytree(fullpath, "destinationpath"+Name2) 

打印“完成”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM