具有文件名和数字的函数

时间:2015-03-05 01:23:11

标签: python python-3.x

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

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

顺便说一句,我不知道错误是什么,它说的只是语法错误。如果有人可以提供帮助,请做。

3 个答案:

答案 0 :(得分:1)

你宁愿使用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()

答案 1 :(得分:1)

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: ')

答案 2 :(得分:1)

我的回答实际上构建了一个函数,询问用户“多个字符”..希望你喜欢它;)

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