[英]How to match and move files into corresponding folders using Python
I am fairly new to programing and this is my first stab at creating a complex script using Python我对编程还很陌生,这是我第一次尝试使用 Python 创建复杂的脚本
The purpose of the program I am creating is:我正在创建的程序的目的是:
Example:例子:
file name: KPHI_SDUS81_N3KDIX_201205261956文件名: KPHI_SDUS81_N3KDIX_201205261956
folder created based on characters: N3K基于字符创建的文件夹: N3K
import os
#Creates a list based on file names in the folder
srcfile=os.listdir("E:\\Learning Python\\Testing out\\thunderstorm stuff")
#Directiory of where the source files are located
srcpath= "E:\\Learning Python\\Testing out\\thunderstorm stuff"
#Creates a list based on the location of where folders will be lcoated.
#List will be empty since for loop has not ran yet
targetsrc=os.listdir("E:\\Learning Python\\Testing out\\test folder")
#path of where the new folders created will be located
targetpath = "E:\\Learning Python\\Testing out\\test folder"
#empty list created to hold a string of 3 characters (see for loop below)
list=[]
#A list to hold the unique string values. (see 2nd for loop below)
Target=[]
#the for loop below looks at a file and gooes to the character place holder index of 12 and looks at the characters UP TO 15.
#It then assigns the three characters to a variable x which then passes the the string (the 3 characters) to an empty list called list.
for num in srcfile:
x=num[12:15]
list.append(x)
#a test to see if the for loop above was able to exact the three characters from the list
print list
print srcfile
#created to see how big the list is which should match the amount of files in folder
print len(srcfile)
print len (list)
#a function created to make a folder based on a list
def create(s):
targetpath = "E:\\Learning Python\\Testing out\\test folder"
test=os.mkdir(os.path.join(targetpath,s))
#a dummy variable holder for the for loop below
valhold = "null"
#a nested if statement inside a for loop.
#The for loop goes through all the string values in a list called "list" (assigned to folder in for loop)
#and checks it against a list called valhold. If folder and valhold are not equal,
#the values in folder are appened to a list called Target.append which holds unique values.
#The next step is to create a folder a folder based off the list value "valhold"
for folder in list:
if folder != valhold:
Target.append(folder)
valhold=folder
create(valhold)
else:
valhold=folder
#a nested for loop which goes through all the files in the folder for the list "sourcefile"
#and finds a matching filename
for dst in Target:
wheretonumber=0
whereto = targetsrc(wheretonumber) #Name of folder for a given index value "targetsrc"
for file in list:
filenumber=0
filename=srcfile(filenumber) #Name of file for a given index value "sourcefile"
if file == dst:
##os.rename(filename(filenumber),whereto(wheretonumber))
##shutil.move(filename,whereto)
filenumber= filenumber+1
wheretonumber=wheretonumber+1
I am able to do the first 2 things in my bullet point list above but having a hard time getting the 3rd one to work.我能够做上面我的要点列表中的前两件事,但很难让第三件事起作用。 I've looked into shutil.move, os.path.walk, and os.rename functions and have had no luck getting them to work.我研究了 shutil.move、os.path.walk 和 os.rename 函数,但没有让它们工作。 I keep getting the error:我不断收到错误:
whereto = targetsrc(wheretonumber) TypeError: 'list' object is not callable whereto = targetsrc(wheretonumber) TypeError: 'list' 对象不可调用
I have the os.rename and shutil.move commented out since I am trying different functions.因为我正在尝试不同的功能,所以我将 os.rename 和 shutil.move 注释掉了。 Is my logic correct in the approach or am I missing something?我的逻辑在方法中是正确的还是我遗漏了什么? Any suggestions on other functions to try or changes to my code to get it to move the files into a folder?关于尝试或更改我的代码以使其将文件移动到文件夹中的其他功能的任何建议?
To remove duplicates from the list
variable just use set()
built-in.要从list
变量中删除重复项,只需使用内置的set()
。 And don't use list
as a variable name, this shadows built-in list()
.并且不要使用list
作为变量名,这会影响内置list()
。
Lists are indexed with brackets []
not parens.列表用括号[]
而非括号索引。
I can't see where you're assigning anything but empty list (you wrote it yourself: #List will be empty since for loop has not ran yet
) to targetsrc.除了空列表(您自己编写的: #List will be empty since for loop has not ran yet
)之外,我看不到您在何处分配任何内容到 targetsrc。 Empty list has no elements, so even L[0] will be out of range.空列表没有元素,所以即使 L[0] 也会超出范围。
Try something like this:尝试这样的事情:
import os
import shutil
srcpath = "E:\\Learning Python\\Testing out\\thunderstorm stuff"
srcfiles = os.listdir(srcpath)
destpath = "E:\\Learning Python\\Testing out\\test folder"
# extract the three letters from filenames and filter out duplicates
destdirs = list(set([filename[12:15] for filename in srcfiles]))
def create(dirname, destpath):
full_path = os.path.join(destpath, dirname)
os.mkdir(full_path)
return full_path
def move(filename, dirpath):
shutil.move(os.path.join(srcpath, filename)
,dirpath)
# create destination directories and store their names along with full paths
targets = [
(folder, create(folder, destpath)) for folder in destdirs
]
for dirname, full_path in targets:
for filename in srcfile:
if dirname == filename[12:15]:
move(filename, full_path)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.