繁体   English   中英

将文本文件中列出的特定文件从源文件夹复制到目标文件夹

[英]Copy the specific files listed in text file from source folder to Destination folder

我有一个格式不同的文本文件中的文件名列表。 我在Source文件夹中找到了多个文件,我想在源文件夹中搜索文件,并在文本文件中使用各自的文件名将这些文件复制并粘贴到目标文件夹中。

范例:

文本文件:仅包含有限的(选定文件)

C:/.../abc.doc::1
C:/.../def.doc::1
c:/.../ghu.doc::1
c:/.../zzz.doc::1

源文件夹:

C:/.../abc.doc
C./.../12a.doc
C:/.../def.doc
c:/.../ghu.doc
c:/.../zzz.doc

目标文件夹:

C:/.../abc.doc
C:/.../def.doc
c:/.../ghu.doc
c:/.../zzz.doc

我是python的新手,我尽了最大的努力,需要一些有价值的输入来完成我的家庭作业

Step1: I like to select the text file
Step2: Slice the line only the file name (C:/.../abc.doc::1) to file name(abc) 
Step3: Search the file name in the source folder
Step4: Copy and paste it to destination folder.

代码:

import os
from tkinter import filedialog
from tkinter import *

root = Tk()
#FolderA = os.path.normpath(filedialog.askdirectory(initialdir="/", title="Select png source path")) + "\\"
text_file_list =  os.path.normpath(filedialog.askopenfilename(initialdir = "/", title="Select Rating text or csv file", filetypes = (("text files","*.txt"), ("all files","*.*"))))
FolderB = os.path.normpath(filedialog.askdirectory(initialdir="/", title="Select png source path")) + "\\"
print (FolderA)
print (FolderB)

os.chdir(text_file_list)

namelist = list()

for f in os.listdir():
    file_name,file_ext = os.path.splitext(f)
    namelist.append(file_name)

os.chdir(FolderB)

for findex, f in enumerate(os.listdir()):
    t = f
    strs.startswith('py') and strs.endswith("27")
    file_name,file_ext = os.path.splitext(f)
    os.rename(f, namelist[findex] + file_ext)
    print(file_name)

从评论中复制:

with open(text_file_list, "r") as ins: 
    array = [] 
    for line in ins: 
        array.append(line) 
        print(line) 
        m = re.search(r"(?<=tinted_combined).*?(?=.jpg::1)", your_text).group(0) 
        if m: 
            found = m.group(1) 
            print(found)

试试这个代码。 希望它能工作。

import os
import shutils

files = [os.path.join(SOURCE_PATH, f) for f in os.listdir(SOURCE_PATH)
                    if os.path.isfile(os.path.join(SOURCE_PATH, f))]
for file in files:
    shutil.move(file, DESTINATION_PATH)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM