选择文件夹Python
我是python新手,希望为文件夹中的特定特征编写代码,然后对所需文件夹中的内容执行操作。下面是一个例子:选择文件夹Python,python,directory,Python,Directory,我是python新手,希望为文件夹中的特定特征编写代码,然后对所需文件夹中的内容执行操作。下面是一个例子: Path = "./Desktop/Example/" #Input Directory A_files = [] for dirName, subdirList, fileList in os.walk(Path):
Path = "./Desktop/Example/" #Input Directory
A_files = []
for dirName, subdirList, fileList in os.walk(Path):
for filename in fileList:
if "[A]" in subdirList()
if ".txt" in filename.lower():
A_files.append(os.path.join(dirName,filename))
#Perform Mathematical Operators for A_files only
B_files = []
for dirName, subdirList, fileList in os.walk(Path):
for filename in fileList:
if "[B]" in subdirList()
if ".txt" in filename.lower():
B_files.append(os.path.join(dirName,filename))
#Perform Mathematical Operators for B_files only
#Perform Mathematical Operators between A_files and B_files together
文件夹示例中有子文件夹:Hello_World[A]和Hello_World[B],我希望首先分别访问每个文件夹中的所有.txt文件,以执行值的缩放。后来我在这两个文件之间做了其他数学运算
谢谢你的帮助 我想你是在要求这样的东西:
Path = "./Desktop/Example/" #Input Directory
A_files = []
for dirName, subdirList, fileList in os.walk(Path):
for filename in fileList:
if "[A]" in dirName:
if ".txt" in filename.lower():
A_files.append(os.path.join(dirName,filename))
#Perform Mathematical Operators for A_files only
B_files = []
for dirName, subdirList, fileList in os.walk(Path):
for filename in fileList:
if "[B]" in dirName:
if ".txt" in filename.lower():
B_files.append(os.path.join(dirName,filename))
#Perform Mathematical Operators for B_files only
for f in B:
data = open(filename).read()
# Do stuff
#Perform Mathematical Operators between A_files and B_files together
for index, file in enumerate(A):
dataA = open(A[i]).read()
dataB = open(B[i]).read()
# Do something
由于dirname是一个字符串,因此可以使用in检查其内容
然后,您可以迭代A[]和B[]进行计算,并使用enumerate使用索引对这两个对象进行迭代。请参见此处:您的问题是什么?如何仅根据名称的一部分选择文件夹?与上一条if语句一样,我只能选择.txt文件,我只想选择itI中包含[A]的文件夹。我认为glob模块可以进行通配符式的部分匹配。我自己也没用过。问题是我知道前缀会随着时间的推移而改变,但我知道[A]约定和[B]约定将保持不变。我需要找到一种方法,通过目录隔离Hello_World[a]和Hi_World[B]