2012-07-12 55 views
1

我正嘗試在certian目錄內創建一個文件的路徑和文件名的一切。環顧四周後,它看起來像要做到這一點,最好的辦法是與GLOB功能,我能寫這讓我的路徑的列表,以每個文件代碼:Beginner Python:將GLOB結果寫入txt

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

但我似乎無法將結果寫入一個文本文件:我似乎可以做的最好的是產生與dirtup一個上市一個文本文件(編輯以顯示代碼w/sugestions):

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

如何將dirtup的所有結果存入文本文件?提前致謝。

+0

您提到的其他帖子與您的問題無關。它處理命令行問題,而您的Python代碼中只存在一個問題。 – 2012-07-12 13:55:10

回答

2

問題是您多次打開該文件。使用'w'標誌,每次打開文件時文件都會被截斷,並且只能看到最後一次寫入。

打開循環之前的文件,之後將其關閉,或使用新發明的具有聲明:

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」

+0

謝謝,那是它!我花了一分鐘才意識到,自從我使用Python 2.5以來,我需要啓用這個語句,但是一旦我做了一切順利運行。 – mburkenysdot 2012-07-12 17:27:21

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

您可能需要在'fo.write(...)'中使用換行符(儘管輸出文件的格式應該是什麼有點不清楚)。 – mgilson 2012-07-12 14:24:58

+0

@mgilson,挺對的!謝謝! – 2012-07-12 14:26:39

+0

@ user1520806 - 嘗試:'fo.write(','.join(dirtup)+'\ n')'。你應該得到多個結果,他們並不是每個都放在一個新的行上。 – mgilson 2012-07-12 15:32:14

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

感謝您在@Daren Thomas的評論中關注我。我應該在我的原始線程中更具體,並說明我爲什麼使用GLOB,因爲我只對某些遵循certian模式的子目錄感興趣,但是,現在我已經知道爲什麼「with」不適合我(使用2.5)我也會嘗試你的解決方案。 – mburkenysdot 2012-07-12 17:25:34