具有文件名和数字的函数

2024-10-06 12:57:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试构建一个函数,它要求用户输入一些字符(称这个数字为n),然后输入一个文件名。脚本应该打开文件,在屏幕上一次显示n个字符的内容,然后关闭文件。如果文件不存在,脚本应该反复要求用户输入不同的文件名。我的代码中似乎有一个bug:

myInput = input
print('please enter a positive integer: ')
myInput = n
try:                                                                           
    opened_file = open(filename)                                               
    chars = opened_file.read(n)                                          
    while chars != "":                                                   
        chars = opened_file.read(n)                                      
        print(chars)                                                     
    opened_file.close()                                                        
    except IOError:                                                           
        print('Please enter a different file name: ')
        input()

顺便说一句,我不知道是什么错误,它说的都是语法错误。如果有人能帮忙,请帮忙。你知道吗


Tags: 文件函数用户脚本readinput文件名数字
3条回答

你宁愿使用sys.stdin而不是input来让python处理字符串编码+许多小错误:

import sys

print('please enter a file name: ')
myInput = sys.stdin.readline()[:-1]

print('please enter a positive integer: ')
n = int(sys.stdin.readline()[:-1]) # guess u mean this

while True: # to retry on fail
    try:
        opened_file = open(myInput) # no variable filename
        chars = opened_file.read(n)
        while chars != "":
            chars = opened_file.read(n)
            print(chars)
        opened_file.close()
        exit() # success exit
    except IOError: # format error
        print('Please enter a different file name: ')
        input()
while True:
    try:
        mynum = int(input('please enter a positive integer: '))
        if mynum >= 0:
            # exit loop for positive integer
            break;

        # loop again for negative integer
        print('must be positive integer')            

    except ValueError as v:
        print("Must enter an integer")

myfile = input('Please enter a file name: ')

while True:
    try:
        with open(myfile, "r") as f:
            chars = f.read(mynum)                                          
            while chars != "":                                                   
                chars = f.read(mynum)                                      
                print(chars)                                                     
        break
    except IOError as e:           
        myfile = input('Please enter a different file name: ')

我的答案实际上构建了一个函数,要求用户输入“许多字符”。。希望您喜欢;)

import os.path
def function_builder():

    def fn():
        n = None
        file_ = None
        while not n:
            n = raw_input('Enter a number of chars: ')

        while True:
            file_ = raw_input('Enter a filename: ')
            if os.path.isfile(file_):
                break

        #pure magic that converts chars to int
        magic_integer_value_of_n = sum(map(ord,n))

        with open(file_) as f: 
          while True:
            c = f.read(an_integer_value_of_n)
            if not c:
                print("\nEnd of file")
                break
            print('\nprinting {} chars'.format(magic_integer_value_of_n))
            print(c)

    return fn

function_builder()()

相关问题 更多 >