2012-07-25 76 views
2

我對Python腳本編程相當陌生,我想驗證目錄和子目錄中的文件名。 驗證應區分大小寫。 我使用python 2.6.5 操作系統:WIN7和XP按標準驗證文件名

我提示以下用戶輸入:

prompt = "year" 
year = raw_input(prompt) 
prompt = "number" 
number = raw_input(prompt) 

在這裏,我想搜索/驗證下面的文件和文件夾存在,其文件名是正確的。

folderstructure:

..\foobar_(number)_version1\music 

文件中的子文件夾「音樂」

(year)_foobar_(number)_isnice.txt 
(year)_itis(number)hot_today.txt 
(year)_anything_is(number)possible.txt 
(year)_something_{idont_want_to_check_this_part}_(number)_canbe_anything.txt 

需要注意的是,包括下劃線所有文字都是一樣的,因此,應該永遠是正確的,除了之間的事( ) 要麼 {}。 我想輸出結果到一個txt文件,該文件報告文件名是否正確。

實現這個最合乎邏輯的方法是什麼? 我已經閱讀了lib文檔fnmatch(.fnmatchcase),RE和os(.path.isfile),並在這裏查找了一些示例,但我無法弄清楚在哪裏以及如何開始。

任何人都可以指向正確的方向嗎?

只要我的腳本有工作基礎,我會發布我的代碼以供參考或幫助其他人。

[EDIT2]我的第一個非的Hello World腳本

import os 
import re 

#output : 
file_out = "H:\\output.txt" 
f_out = open(file_out, 'w') 

print "-------start-script----------" 

#input 
prompt = "enter 4 digit year: " 
year = raw_input(prompt) 
prompt = "enter 2 digit number: " 
number = raw_input(prompt) 

print "the chosen year is %s" % (year) 
print "the chosen number is %s" % (number) 

f_out.write ("start log!\n") 
f_out.write ("------------------------------------------\n") 
f_out.write ("the chosen year is %s\n" % (year)) 
f_out.write ("the chosen number is %s\n" % (number)) 

#part i'm working on 

print "end script" 
f_out.write ("------------------------------------------\n") 
f_out.write ("end script\n") 

#close file 
f_out.close() 

回答

2

看看glob模塊 - 這將幫助你在當前目錄中獲取文件的列表:

import glob 

year = raw_input('Year: ')  # Example: Year: 2009 
number = raw_input('Number: ') # Example: Number: 12 
filenames = glob.glob('{year}_*{number}*'.format(year=year, number=number)) 

名的文件將在滿足以下條件的當前目錄什麼:

  1. 2009_
  2. 開頭的任何數目的字符,直到它匹配12
  3. 以下12任何數量的字符。

os.path.exists是檢查文件是否存在,或者os.path.isfile如果你想確保它確實是一個文件,而不是像命名的文件目錄的好辦法。對於Python3,請檢查these docs,並且如link ghostbust555 mentioned所述,如果您計劃除了驗證其存在性之外,還要注意競爭條件。


根據你的評論,它看起來像這是一個正則表達式的工作。你需要寫什麼的僞代碼看起來是這樣的:從實際的模式

for filename in list of filenames: 
    if filename is not valid: 
     print "<filename> is not valid!" 

除此之外,實際的Python代碼看起來是這樣的:

import os 
import re 

pattern = 'Put your actual pattern here' 

# For a different directory, change the . to whatever the directory should be 
for filename in os.listdir('.'): 
    if not re.match(pattern, filename): 
     print("Bad filename: ", filename) 
+0

嗨,謝謝你的回答。 但我想確保年份和號碼之間的一切都是正確的。例如,在文件「(年)_foobar_(number)_isnice.txt」中,也應檢查部分「_foobar_」和「_isnice.txt」。 因此,如果我有一個像「(年)_foobar_(number)_isbad.txt」的文件,它應該報告它是不正確的(因爲它不符合所需的部分「_isnice.txt」 希望我解釋得很對,英語不是我的主要語言。 – Ruud 2012-07-25 15:22:38

+0

嗨,你可以發佈一個使用模式字符串中的兩個raw_inputs(年份,數字)的例子嗎? 我搜索的例子,但無法找到或得到任何工作。我需要re.compile/re.group部分嗎? – Ruud 2012-07-26 15:01:20

+0

@Ruud,'re.compile'只是讓正則表達式運行得更快。如果你爲大數目(> 1000?)做了這個,那麼你可以嘗試使用re.compile作爲模式。我會先運行它,如果它看起來很慢,你可以嘗試優化。 're.group'只顯示比賽的不同部分 - 在這種情況下,你只關心你的整個模式匹配。我剛剛修改我的第一個示例,使用'raw_input'來獲取年份/編號。 – 2012-07-27 13:11:39

-1
import os.path 

year = 2009 
file1 = year + "_foobar_" + number + "_isnice.txt" 

os.path.exists(file1) 
+0

由於文件不存在以外,IOError可能會因爲多種原因而飛行。 – Kos 2012-07-25 15:12:56

+0

雖然需要注意的是,只有在運行腳本的人可以讀取文件的權限時,這才能正常工作。 – 2012-07-25 15:13:01

+0

@Kos是真實的,但根據此線程 - http://stackoverflow.com/questions/82831/how-do-i-check-if-a-file-exists-using-python os.path.exists()可能會導致潛在的安全漏洞 – ghostbust555 2012-07-25 15:17:32

0

這並不意味着是一個完整答案,但@Wayne Werner的答案的延伸。我沒有足夠的聲譽點評論。 ; 0

韋恩的使用格式的方法我認爲是指向你應該做什麼,因爲它是 驗證文件名之前,文件建成,而不是之後。這似乎就是你在做什麼,並控制了?

  1. 我會盡可能多地在用戶輸入級別進行驗證。
  2. 確認您獲得它們的其他部分。
  3. 用零件構建字典。
  4. 建立你的file_name。

例如,在用戶輸入電平,是這樣的:

yourDict = dict() 

year_input = raw_input('What is the year'?) 

if not year_input.isdigit(): 
    year_input = raw_input('Only digits please in the format YYYY, example: 2012'): 

yourDict[year] = year_input 

然後continute重點補充:由這是什麼標準,你必須驗證其他值值yourDict。 (使用重新模塊或其他提到的方法)。

然後,正如Wayne所做的那樣,使用帶有傳入字典的.format()映射到正確的部分。

format1 = "{year}{part1}{number}{part2}.txt".format(**yourDict) 

該方法還允許您快速建立新的格式,具有相同的部件,你可以在你需要或不需要每種格式的詞典挑選哪些鍵。

希望有幫助。