2

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

我提示输入以下用户输入:

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

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

文件夹结构:

..\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()
4

3 回答 3

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,请查看这些文档,并且就像提到的 ghostbust555 链接所说的那样,如果您除了验证它们的存在之外还打算做任何事情,请注意竞争条件。


根据您的评论,看起来这是正则表达式的工作。您需要编写的伪代码如下所示:

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)
于 2012-07-25T15:17:54.617 回答
0

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

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

  1. 我会在用户输入级别做尽可能多的验证。
  2. 验证从任何地方获得的其他部分。
  3. 用零件建立一个字典。
  4. 建立你的文件名。

例如,在用户输入级别,类似:

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

然后继续将 key:values 添加到 yourDict,方法是根据您拥有的任何标准验证其他值。(使用 re 模块或提到的其他方法)。

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

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

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

希望这会有所帮助。

于 2012-07-25T15:58:03.227 回答
-1
import os.path

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

os.path.exists(file1)   
于 2012-07-25T15:10:12.637 回答