简体   繁体   English

使用python将文件复制到名称与该文件几乎相同的文件夹

[英]Using python, copy a file to a folder that has almost the same name as the file

I will try to explain what I'm trying to do, but English isn't my first language. 我将尝试解释我要做什么,但是英语不是我的母语。

I have a list of items, eg ["cat size", "house size", "car size"] and some files that contain the names of the items in my list. 我有一个项目列表,例如["cat size", "house size", "car size"]和一些包含列表中项目名称的文件。 eg cat.size.txt and house.size.fromBob.txt . 例如cat.size.txthouse.size.fromBob.txt

I want to do the following copy: cat.size.txt to directoty H:/new/2CopyTo/Cat size/ and house.size.fromBob.txt to directory H:/new/2CopyTo/house size/ 我要执行以下复制:将cat.size.txt为目录H:/new/2CopyTo/Cat size/并将house.size.fromBob.txt到目录H:/new/2CopyTo/house size/

Below is what I made to copy the files to just one directory, but I want each file in its own directory (on my pc the list contains 50 items, so don't want to do it manually). 以下是我将文件复制到一个目录中的操作,但是我希望每个文件都位于其自己的目录中(在我的电脑上,该列表包含50个项目,因此不想手动进行操作)。

Thanks!! 谢谢!!

import glob
import shutil
import os

#=========List of items
list = ["cat size","house size","car size"]

#=========directories
FromDir = "H:/New/1RecievedFiles/"
CopyDir = "H:/New/2CopyTo/"

#=========get list of items to be used in glob.glob
filesList = [x.replace(" ", "*")+"*.*" for x in list]

#=========get list of new files in FromDir, use filesList in glob.glob
listOfNew = [glob.glob(FromDir+item) for item in filesList ]
# returns [['H:/New/1RecievedFiles\\cat.size.txt'], ['H:/New/1RecievedFiles\\house.size.fromBob.txt']]    

#=========make a single list from the list in a list
listOfNewfiles = [item for sublist in listOfNew for item in sublist]
# returns ['H:/New/1RecievedFiles\\cat.size.txt', 'H:/New/1RecievedFiles\\house.size.fromBob.txt']

#=========copy files
[shutil.copy2(item, CopyDir) for item in listOfNewfiles]

I set up a test directory that looks like this: 我设置了一个测试目录,如下所示:

$ ls -1
copyto/
car.size.txt
cat.size.txt
house.size.fromBob.txt

I believe this is similar to what you have. 我相信这与您拥有的类似。 Given the same list you have, here is how I approached your problem: 给定与您相同的清单,以下是我处理您的问题的方法:

import os, shutil
# lst not list -- naming it list overshadows a builtin
lst = ['cat size', 'house size', 'car size']
# PEP8 - lowercase with underscore here; CamelCase for classes
from_dir = os.path.abspath(os.path.expanduser('~/test'))
copy_dir = os.path.abspath(os.path.expanduser('~/test/copyto'))

to_copy = [f for f in os.listdir(from_dir) if os.path.isfile(f)]
# ['car.size.txt', 'house.size.fromBob.txt', 'cat.size.txt']

for filename in to_copy:
    needle = ' '.join(filename.split('.')[:2])
    if needle in lst:
        dest_dir = os.path.join(copy_dir, needle)
        os.mkdir(dest_dir)
        shutil.copy2(os.path.join(from_dir, filename), dest_dir)

After this, my test directory was exactly as shown before, and this is the result of ls -R1 copyto : 此后,我的test目录与之前显示的完全相同,这是ls -R1 copyto的结果:

copyto:
car size/
cat size/
house size/

copyto/car size:
car.size.txt

copyto/cat size:
cat.size.txt

copyto/house size:
house.size.fromBob.txt

Does that work for your case? 这对您的情况有用吗? (PS - Python is great for this kind of thing, isn't it?) (PS-Python非常适合这种事情,不是吗?)

Iterate through your list and check for the term that categorizes where its going to be saved. 遍历您的列表,并检查将其保存在何处的术语。 IE 'cat', 'dog', or 'house' IE“猫”,“狗”或“房子”

something like this 像这样的东西

import os,shutil

for item in list:
    if 'cat' in listitem:
        shutil.copyfile(source, 'H:/new/2CopyTo/cat size/')
    elif 'house' in listitem:
        shutil.copyfile(source, 'H.......etc etc
    elif 'car'.....

is this what you were wanting? 这就是你想要的吗?

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

相关问题 使用python查找几乎相似的名称文件 - Finding almost similar name file using python 在Python中生成几乎随机的文件名 - Generate almost random file name in Python 用同一个文件夹python中的第一个文件的名称命名一个文件夹 - Naming a folder with the name of the first file in the same folder python 将具有特定名称的文件从一个文件夹复制到 python 中的另一个文件夹 - Copy file with specific name from one folder to another in python 有没有办法使用 python 中该文件名的子文本将文件从一个文件夹复制到另一个文件夹? - Is there a way to copy files from one folder to another using a subtext of that file name in python? 将文件移动到恰好在Python中具有相同文件名的新文件夹中 - Move file to a new folder that happens to have the same file name in Python Python:将文件名与同一目录中的文件夹名进行比较 - Python : Comparing file name with folder name in same directory Python 在搜索框中输入名称并在同名文件夹中查找文件 - Python input name in searchbox and find file in folder with same name Python:如何将具有特定名称的文件移动到具有相同名称的文件夹中 - Python: how to move a file with a certain name to a folder with the same name 运行同一文件夹中的文件,使用 python 中的变量 - Run a file in the same folder, using a variable in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM