如何限制在python中检查文件名?

时间:2014-03-16 05:57:43

标签: python

我正在尝试编写一个要求输入特定文件名的程序,然后处理输入文件名以从中创建一个列表。限制是用户只有三次机会输入正确的文件名。这是我的代码:

    import os.path

def file_name_tries():
    for i in range(3):
        filename = input('please enter filename: ')
    try:
        infile = open(filename,'r')
        # Do whatever you want, e.g.
        #print infile.read()
        return infile
        exit()
    except IOError:
        if not os.path.exists(filename): print ('File does not exist')
        else: print ('Error opening file')

    print ('you have exceed your three tries' )   
def process_file():
    #file_name=input('file name: ')
    #input_file=open(file_name,'r')
    input_file=file_name_tries()
    input_list=[]
    #for loop strips each line of end characters
    #and splits each line of the input file at ; and turns the line to a list
    for line in input_file:
        line_list=line.strip().split(';')
        #appends each line_list to input_list
        input_list.append(line_list)
    print( input_list)
process_file()

ERROR:

please enter filename: mlb.txt
please enter filename: mlb.txt
please enter filename: mlb.txt
File does not exist
you have exceed your three tries
Traceback (most recent call last):
  File "C:\Users\Dasinator\Documents\Books IX\Python Examples\textbook examples\project   07\passwordenter3times.py", line 29, in <module>
    open_file()
  File "C:\Users\Dasinator\Documents\Books IX\Python Examples\textbook examples\project 07\passwordenter3times.py", line 24, in open_file
    for line in input_file:
TypeError: 'NoneType' object is not iterable

我将不胜感激任何建议。感谢

3 个答案:

答案 0 :(得分:1)

将其另存为test.py

import os
for i in range(3):
    filename = raw_input('please enter filename: ')
    try:
        infile = open(filename,'r')
        # Do whatever you want, e.g.
        print infile.read()
        exit()
    except IOError:
        if not os.path.exists(filename): print 'File does not exist'
        else: print 'Error opening file'

print 'you have exceed your three tries'

以下是它在终端上的工作原理:

$ rm test.txt
$ python test.py
please enter filename: test.txt
File does not exist
please enter filename: test.txt
File does not exist
please enter filename: test.txt
File does not exist
you have exceed your three tries
$ echo "this is a foo bar" > test.txt
$ python test.py
please enter filename: test.txt
this is a foo bar

答案 1 :(得分:0)

只需在用户成功break时替换return file_open(并删除底部的返回,这会导致错误)。

如果用户三次失败,您的函数将返回None。

答案 2 :(得分:0)

为了“功能化”最多三次尝试文件打开,请尝试:

import os

def three_tries():
    for i in range(3):
        filename = raw_input('please enter filename: ')
        try:
            infile = open(filename,'r')
            return infile
        except IOError:
            if not os.path.exists(filename): print 'File does not exist'
            else: print 'Error opening file'
    print '\nyou have exceed your three tries'
    print 'program ending... byebye...'
    exit()

print 'this is a foo bar script'
print 'i am going to ask the user for a filename'
infile = three_tries()
# Do whatever you want with the file, e.g.
print '\nreading file...'
print infile.read()

在终端上:

$ rm test.txt
$ python test.py
this is a foo bar script
i am going to ask the user for a filename
please enter filename: test.txt
File does not exist
please enter filename: test.txt
File does not exist
please enter filename: test.txt
File does not exist

you have exceed your three tries
program ending... byebye...
$ echo 'foo bar sentence, blah blah' > test.txt
$ python test.py
this is a foo bar script
i am going to ask the user for a filename
please enter filename: test.txt

reading file...
foo bar sentence, blah blah

如果您不希望程序在超过三次尝试后结束:

import os

def three_tries():
    for i in range(3):
        filename = raw_input('please enter filename: ')
        try:
            infile = open(filename,'r')
            return infile
        except IOError:
            if not os.path.exists(filename): print 'File does not exist'
            else: print 'Error opening file'
    print '\nyou have exceed your three tries'
    print 'continuing without reading any file...'
    return

print 'this is a foo bar script'
print 'i am going to ask the user for a filename'
infile = three_tries()

if infile:
    # Do whatever you want with the file, e.g.
    print '\nreading file...'
    print infile.read()
else:
    print 'no file read, continuing to other stuff...'

print 'now you can continues to other parts of the program...'