简体   繁体   English

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

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

I have a list of file names in a text file with different format. 我有一个格式不同的文本文件中的文件名列表。 I have multiple files found in the Source folder , I like to search files in the source folder with respective file name in text file copy those file and paste it to destination folder. 我在Source文件夹中找到了多个文件,我想在源文件夹中搜索文件,并在文本文件中使用各自的文件名将这些文件复制并粘贴到目标文件夹中。

Example : 范例:

Text File : contains only limited (Selected files) 文本文件:仅包含有限的(选定文件)

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

Source Folder : 源文件夹:

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

Destination Folder : 目标文件夹:

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

I am new to python , I tried my level best, need some valuable input to finish my home work 我是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.

Code : 代码:

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)

Copy from comment: 从评论中复制:

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)

Try this code. 试试这个代码。 Hope it works. 希望它能工作。

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.

相关问题 尝试将具有特定扩展名的文件从源文件夹复制到目标文件夹 - Trying to copy files with a specific extension from source folder to destination folder 将(文件)名称末尾带有特定文本的文件从一个文件夹复制到另一个文件夹 - Copy files with specific text at the end of their (file) names from one folder to other folder 如果字符串等于文件文件夹,则将文件复制到目标文件夹 - Copy Files to Destination Folder if String Equals File Folder 将指定数量的文件从源文件夹移动到目标文件夹 - Move specified number of files from source folder to destination folder 如何仅将文本文件中列出的图像从一个文件夹复制到另一个文件夹而忽略丢失的图像? - How to copy only the images listed in a text file from one folder to another while ignoring the missing images? 将文件从源文件夹复制到其他目标文件夹 - Copying files from source folder to different destination folders 不工作:将在.csv 文件中找到的文件复制到列表中,然后如果在某个文件夹中,则复制到目标文件夹 - not working: copy files found in .csv file to list, then if in certain folder, copy to destination folder 如何在python中将文本文件中列出的文件从一个文件夹移动到另一个文件夹 - How to move files listed in a text file from one folder to another in python 将文件移动到文件夹或进行重命名的副本(如果目标文件夹中存在) - Move file to a folder or make a renamed copy if it exists in the destination folder 有没有办法将随机文件从源复制到目标? - Is there a way to copy random files from source to destination?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM