[英]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.txt
和house.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.