我有一系列包含在主文件夹中的子文件夹。我想访问每个子文件夹,以便我可以使用它中的文件。我试过以下,但显然我的'追加'方法是不可能在Python上。有人可以帮我吗?打开其他文件夹中包含的文件夹
import os
os.chdir('C\\current folder')
for subfolder in os.listdir(os.getcwd()):
os.chdir('C\\current folder\\'subfolder'')
我有一系列包含在主文件夹中的子文件夹。我想访问每个子文件夹,以便我可以使用它中的文件。我试过以下,但显然我的'追加'方法是不可能在Python上。有人可以帮我吗?打开其他文件夹中包含的文件夹
import os
os.chdir('C\\current folder')
for subfolder in os.listdir(os.getcwd()):
os.chdir('C\\current folder\\'subfolder'')
import os.path
import glob
for subdir in glob.glob("X/certain*"):
path = os.path.join(subdir, "result.txt")
with open(path, 'w') as fp:
fp.write("This is the result\n")
此代码帮助我从这里迈出了lot.Actually ....但他是一个生命的救星..!
How do I open a folder that contains files within another folder
这里就是我要寻找的.png
文件中的所有目录下递归地path_to_my_root_directory
并追加的所有文件和路径的列表,列表,名为fis
一个例子。
import os
fis=[]
rootdir=os.walk('path_to_my_root_directory`)
for root,subd,fils in rootdir :
for name in fils:
if name.endswith('.png'):
fis.append(os.path.join(root,name))
'os.walk()'是你的朋友 – suvy
的[导航与Python文件夹]可能的复制(https://stackoverflow.com/questions/33549660/navigate-folders-with-python) – AlG