我处理的文件夹层次结构如下所示:
c:/users/rox/halogen/iodine/(some .txt files)
c:/users/rox/halogen/chlorine/(some .txt files)
c:/users/rox/inert/helium/(some .txt files)
c:/users/rox/inert/argon/(some .txt files)现在,我使用os.walk遍历文件夹并处理文件。
但问题是,如果我想在分析了卤素下的所有子文件夹后,生成分析输出到文件夹' halogen‘,那么我应该怎么办……我使用的是:
for root,dirs,files in os.walk(path,'*.txt):
.....
.......[processing file]
out.write(.....) # writing output in the folder which we are analyzing但是如何将输出写入后退两步的文件夹(即卤素或惰性)。
发布于 2012-06-06 02:32:29
您可以使用正在处理的目录中的相对路径打开输出文件,如下所示:
for root, dirs, files in os.walk(path, '*.txt'):
out = open(os.path.join(root, '..', '..'), 'a')
out.write(...)发布于 2012-06-06 02:27:54
在漫游之前打开输出文件。
out = open(os.path.join(path, outputfilename), 'w'),然后学习处理输入的方法。
for root,dirs,files in os.walk(path,'*.txt):
.....
out.write(..)这样,您就已经知道了根路径。否则,如果您确定您的路径只是后退两步。
os.path.join(current_path, '..', '..')将为您提供文件夹路径,后退两步
https://stackoverflow.com/questions/10902695
复制相似问题