Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python初学者:将GLOB结果写入txt_Python_Glob_Python 2.5 - Fatal编程技术网

Python初学者:将GLOB结果写入txt

Python初学者:将GLOB结果写入txt,python,glob,python-2.5,Python,Glob,Python 2.5,我正在尝试创建一个文本文件,其中包含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.

我正在尝试创建一个文本文件,其中包含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()
然而,我发现自己在使用excell的中间语句从报告中提取值。理想情况下,我希望将文件的basename作为元组包含在路径中。我得出了以下结论:

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
但我似乎无法将结果写入文本文件:我能做的最好的事情似乎是使用dirtup中的一个列表生成一个文本文件(编辑以显示代码w/sugestions):


如何将dirtup的所有结果放入文本文件?提前谢谢。

我看不出你的问题:

>>> 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))
我认为这样更好:

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)))

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

在循环之前打开文件,然后在循环之后关闭,或者使用新的fangled 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')
退出该代码块时,文件将自动关闭


这是您错过的参考:

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


你提到的另一篇文章与你的问题无关。它处理命令行问题,而您的Python代码中只有一个问题。您可能希望在
fo.write(…)
中有一个换行符(尽管输出文件的格式有点不清楚)。@user1520806--try:
fo.write(','.join(dirtup)+'\n')
。您应该编写多个结果,它们只是没有分别放在新行上。@Mglison:我一直在尝试发布我使用的代码,该代码只返回了第一个结果,但遇到了格式错误,因此如果我的注释有问题,我会道歉。我可以按照我的第一个例子的语法吗?还是我的问题来自于这个语法?感谢您在@Daren Thomas的评论中跟进我。我应该在我的原始线程中更具体地说明我为什么使用GLOB,因为我只对遵循特定模式的某些子目录感兴趣,然而,现在我已经知道为什么“with”对我不起作用(使用2.5),我也会尝试你的解决方案。谢谢你,成功了!我花了一分钟的时间才意识到我需要启用with station,因为我目前正在使用Python 2.5,但一旦我做到了,一切都顺利运行。
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')