1

我正在尝试创建一个 certian 目录中所有内容的路径和文件名的文本文件。环顾四周,看起来最好的方法是使用 GLOB 函数,我能够编写一个代码,让我得到每个文件的路径列表:

import glob
dirlist = glob.glob('P:\MyDep\Myunit\MySection\Stuff\Reports\YR*\O*\*\*\*\*.pdf')
Bureau\Traffic\Reports\YR*\R*\*\*.pdf')
fo=open('new.txt', "w")
fo.write('\n'.join(dirlist))
fo.close()

但是,我发现自己使用 Excel 的 MID 语句从报告中提取值。理想情况下,我希望文件的基本名称包含在路径中作为元组。我想出了以下几点:

for f in glob.glob('P:\MyDep\Myunit\MySection\Stuff\Reports\YR*\O*\*\*\*\*.pdf'):
        fpath, fname = os.path.split(f)
        rname, extname = os.path.splitext(fname)
        dirtup = (f, rname)
        print dirtup

但我似乎无法将结果写入文本文件:我似乎能做的最好的事情是生成一个文本文件,其中包含一个来自污垢的列表(编辑以显示带有建议的代码):

import glob, os
for f in f in glob.glob('P:\MyDep\Myunit\Mysection\Stuff\Reports\YR*\O*\*\*\*\*.pdf'):
    fpath, fname = os.path.split(f)
    rname, extname = os.path.splitext(fname)
    dirtup = (f, rname)
    fo=open('axlspd.txt', "w")
    fo.write(', '.join(dirtup)+'\n')
    fo.close()

我怎样才能把弄脏的所有结果放到一个文本文件中?提前致谢。

4

3 回答 3

2

问题是您多次打开该文件。使用 'w' 标志,文件在每次打开时都会被截断,您只会看到最后一次写入。

在循环之前打开文件,然后关闭它,或者使用新奇的 with 语句:

import glob, os
with open('axlspd.txt', "w") as axlspd:
    for f in f in glob.glob('P:\MyDep\Myunit\Mysection\Stuff\Reports\YR*\O*\*\*\*\*.pdf'):
        fpath, fname = os.path.split(f)
        rname, extname = os.path.splitext(fname)
        dirtup = (f, rname)
        axlspd.write(', '.join(dirtup)+'\n')

当您退出该代码块时,该文件会自动关闭。


这是您错过的参考:http: //docs.python.org/library/functions.html#open

最常用的模式值是“r”表示读取,“w”表示写入(如果文件已经存在,则截断文件)

于 2012-07-12T16:07:12.613 回答
1

我认为os.walk对此更好:

import os
for root,dirs,files in os.walk('.'):
   #                            ^ Name of directory where you start your file search
   for f in files:
       print root,os.path.join(root,f)  #writes it as path path/filename

看起来您只想要以“.pdf”结尾的文件,因此,我们可以先过滤文件名——将其放入文本文件也很容易:

import os
with open('filelist.txt','w') as outfile:
    for root,dirs,files in os.walk('.'):
       pdfs = [f for f in files if f.endswith('.pdf')]
       for f in pdfs:
           outfile.write('%s %s\n'%(root,os.path.join(root,f)))  #writes it as path path/filename
           #New style formatting:
           #outfile.write('{0} {1}\n'.format(root,os.path.join(root,f)))
于 2012-07-12T14:12:02.333 回答
0

我没有看到你的问题:

>>> import glob
>>> for f in glob.glob(r'c:\*.dll'):
...     print f
...
c:\install.res.1028.dll
c:\install.res.1031.dll
c:\install.res.1033.dll
c:\install.res.1036.dll
c:\install.res.1040.dll
c:\install.res.1041.dll
c:\install.res.1042.dll
c:\install.res.2052.dll
c:\install.res.3082.dll
c:\msdia80.dll
>>> import os
>>> for f in glob.glob(r'c:\*.dll'):
...    fpath, fname = os.path.split(f)
...    rname, extname = os.path.splitext(fname)
...    dirtup = (f, rname)
...    print dirtup
...
('c:\\install.res.1028.dll', 'install.res.1028')
('c:\\install.res.1031.dll', 'install.res.1031')
('c:\\install.res.1033.dll', 'install.res.1033')
('c:\\install.res.1036.dll', 'install.res.1036')
('c:\\install.res.1040.dll', 'install.res.1040')
('c:\\install.res.1041.dll', 'install.res.1041')
('c:\\install.res.1042.dll', 'install.res.1042')
('c:\\install.res.2052.dll', 'install.res.2052')
('c:\\install.res.3082.dll', 'install.res.3082')
('c:\\msdia80.dll', 'msdia80')
>>>

除非您在将元组写入文件时遇到问题。试试这个:

# inside for loop
fo.write(', '.join(dirtup))
于 2012-07-12T13:53:33.807 回答