2012-07-25 71 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) 

该方法还允许您快速建立新的格式,具有相同的部件,你可以在你需要或不需要每种格式的词典挑选哪些键。

希望有帮助。