【问题标题】:How can I open and read an input file and print it to an output file in Python?如何打开和读取输入文件并将其打印到 Python 中的输出文件?
【发布时间】:2019-07-17 04:47:36
【问题描述】:

那么我怎样才能要求用户向我提供一个输入文件和一个输出文件呢? 我希望用户提供的输入文件中的内容打印到用户提供的输出文件中。在这种情况下,用户会输入这个

Enter the input file name: copyFrom.txt
Enter the output file name: copyTo.txt

输入文件中只有文本"hello world"

谢谢。请尽可能保持简单

【问题讨论】:

标签: python file input output


【解决方案1】:

如果你只想复制文件,shutil 的复制文件会隐式循环:

import os
from shutil import copyfile

openfile = input('Enter the input file name:')
outputfile = input('Enter the output file name:')

copyfile(openfile, outputfile)

这个帖子How do I copy a file in Python?了解更多详情

【讨论】:

    【解决方案2】:

    这是一个应该在 Python3 中工作的示例。输入和输出文件名需要包含完整路径(即“/foo/bar/file.txt”

    import os
    input_file = input('Enter the input file name: ')
    output_file = input('Enter the output file name: ')
    
    def update_file(input_file, output_file):
        try:
            if os.path.exists(input_file):
                input_fh = open(input_file, 'r')
                contents = input_fh.readlines()
                input_fh.close()
                line_length = len(contents)
                delim = ''
                if line_length >= 1:
                    formatted_contents = delim.join(contents)
                    output_fh = open(output_file, 'w')
                    output_fh.write(formatted_contents)
                    output_fh.close()
                print('Update operation completed successfully')
        except IOError:
            print(f'error occurred trying to read the file {input_fh}')
    
    update_file(input_file, output_file)
    

    【讨论】:

    • 这仅适用于从输入文件读取单行并将其写入输出文件。尝试按照问题中的要求对文件的全部内容进行这项工作:)
    • @AshutoshPathak 好点!我根据 copyFrom 文件更新了一行或多行
    【解决方案3】:

    你可以这样做...

    import os
    openfile = input('Enter the input file name:')
    outputfile = input('Enter the output file name:')
    if os.path.isfile(openfile):
        file = open(openfile,'r')
        output = open(outputfile,'w+')
        output.write(file.read())
        print('File written')
        exit()
    print('Origin file does not exists.')
    

    【讨论】:

    • 你也可以写一个条件来检查输出文件是否已经存在;如果是,则将输入文件的内容附加到输出文件中。
    • 条件不是必须的,output = open(outputfile,'w+') 可以。
    • 没看到,我的错!
    【解决方案4】:

    要输入输入文件和输出文件名,只需使用input(s) 函数,其中s 是输入消息。

    要获取“用户提供的输入文件中的内容打印到输出文件”,这意味着读取输入文件并将读取的数据写入输出文件。

    要读取输入文件,请使用f = open(input_filename, 'r'),其中第一个参数是文件名,第二个参数是打开模式,'r' 表示读取。然后让readtext作为输入文件的读取文本信息,使用readtext = f.read():这样就返回了f的整个文本内容。

    要将读取的内容输出到输出文件,请使用g = open(output_filename, 'w'),注意现在第二个参数是'w',表示写入。要写入数据,请使用g.write(readtext)

    请注意,如果输入文件未找到或输出文件无效或目前无法输出,则会引发异常。要处理这些异常,请使用 try-except 块。

    这实际上是 Python 中的文件复制操作。 shutil 可以作为一个有用的替代方案。

    【讨论】:

      【解决方案5】:

      首先您必须读取文件并将其保存到某个变量(此处为rd_data):

      if os.path.exists(input_file_name):
              f = open(input_file_name,"r")
              rd_data = f.read()
              f.close()
      

      然后你必须将变量写入其他文件:

      f = open(output_file_name,"w")
      f.write(rd_data)
      f.close()
      

      完整代码如下:

      import os
      
      input_file_name = input("Enter file name to read: ")
      output_file_name = input("Enter file name to write: ")
      if os.path.exists(input_file_name):
          f = open(input_file_name,"r")
          rd_data = f.read()
          f.close()
      
      f = open(output_file_name,"w")
      f.write(rd_data)
      f.close()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-12-19
        • 1970-01-01
        • 2017-11-04
        • 1970-01-01
        • 2017-10-20
        • 1970-01-01
        • 2016-06-26
        相关资源
        最近更新 更多