文件myfilepaths
的內容是:使用水珠完成的文件名(蟒蛇)
/current/0.e_head/10000000d.000000002__head
/current/0.13_head/10000008.000000001__head
..(cont'd)
的實際文件名是:
/current/0.e_head/10000000d.000000002__head_918293
/current/0.13_head/10000008.000000001__head_9891283
..(cont'd)
我的代碼是:
filepath = '/path/to/directory/myfilepaths'
location = raw_input('Where is the file? ')
with open(filepath, 'r') as mf:
for line in mf:
nline = line.replace("\n", "")
# cline = glob.glob(nline + '*') #PROBLEM LINE
path_obj = os.path.join(location + str(nline))
obj_stats = os.stat(path_obj)
我期望做的是採用glob來爲我完成文件名,因爲文件名的末尾不是確定性的呃它是硬編碼的。通過創建的raw_input的location
通常是這樣的/ var /本地/等
目前,如果我在這裏使用粘貼代碼,我得到錯誤信息:
no such file or directory: '/var/local/0.e_head/1000000d.000000002_head'
如果我刪除發表意見,並在path_obj
與cline
取代nline
我得到:
OSERROR:[錯誤2]沒有這樣的文件或目錄「的/ var /本地[]」
現在,我已經看到了很多SO它展示瞭如何Ť o使用glob匹配某些文件,例如* .txt等,但我沒有看到任何使用glob來查找搜索條件的結尾
glob返回一個列表,你可以不使用'__head'和模式來找到它們全部或者是否有一些你不想要的? –
@PadraicCunningham - 是的,不幸的是,有一些文件名是相似的,但不相關,這就是爲什麼選擇的文件正在從輸入文件中讀取以進行過濾。 – user3019191
我添加了一個答案,試試看看你如何去。如果路徑是正確的,並且文件存在,它應該可以正常工作 –