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

2024-04-25 00:07:00 发布

您现在位置: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()

错误:

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

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


Tags: nameinforinputlinenotopenfilename
3条回答

只要在用户成功时用return file_open替换您的break(并删除底部的返回,这是导致错误的原因)。你知道吗

如果用户失败了三次,那么函数将返回None。你知道吗

另存为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

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

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...'

相关问题 更多 >