按标准验证文件名

时间:2012-07-25 15:02:00

标签: python

我是python脚本的新手,我想验证目录和子目录中的文件名。 验证应区分大小写。 我正在使用python 2.6.5 操作系统:win7和xp

我提示输入以下用户:

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

从这里,我想搜索/验证以下文件和文件夹是否存在且文件名是否正确。

folderstructure:

..\foobar_(number)_version1\music

子文件夹'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世界脚本

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

3 个答案:

答案 0 :(得分: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. 开始
  3. 12
  4. 匹配之前的任意数量的字符
  5. 12后的任意数量的字符。
  6. 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)
    

答案 1 :(得分:0)

这并不是一个完整的答案,而是@Wayne Werner答案的延伸。我还没有足够的声誉点来发表评论。 0

Wayne使用格式的方法我认为是指向你应该做的事情,因为它是 在构建文件之前验证文件名而不是之后。似乎这就是你正在做的事情并且能够控制住它?

  1. 我会尽可能在用户输入级别进行验证。
  2. 从您获得的其他部分验证其他部分。
  3. 使用部件构建字典。
  4. 构建您的file_name。
  5. 例如,在用户输入级别,例如:

    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
    

    然后通过按照您拥有的任何条件验证其他值,继续向yourDict添加key:值。 (使用re模块或其他提到的方法)。

    然后,正如Wayne所做的那样,使用带有传入字典的.format()来映射到正确的部分。

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

    该方法还允许您使用相同的部分快速构建新格式,并且您可以选择字典中您需要或不需要的每种格式的键。

    希望这有帮助。

答案 2 :(得分:-1)

import os.path

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

os.path.exists(file1)