How can I open and read an input file and print it to an output file in Python?

自作多情 提交于 2021-01-28 08:28:17

问题


So how can I ask the user to provide me with an input file and an output file? I want the content inside the input file provided by the user to print into the output file the user provided. In this case, the user would put in this

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

inside the input file is just the text "hello world".

Thanks. Please keep it as simple as you can if possible


回答1:


If you just want to copy the file, shutil’s copy file does the loop implicitly:

import os
from shutil import copyfile

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

copyfile(openfile, outputfile)

This this post How do I copy a file in Python? for more detail




回答2:


Here is an example that should work in Python3. The input and output file names would need to include the full path (i.e. "/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)



回答3:


You can do this...

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



回答4:


To input the input-file and output-file names, simply use the input(s) function where s is the input message.

To get the "content inside the input file provided by the user to print into the output file," that would mean reading the input file and writing the read data into the output file.

To read the input file, use f = open(input_filename, 'r'), where the first argument is the filename and the second argument is the open mode where 'r' means read. Then letting readtext be the read text information of the input file, use readtext = f.read(): this returns the entire text content of f.

To output the read content to the output file, use g = open(output_filename, 'w'), noting that now the second argument is 'w', meaning write. To write the data, use g.write(readtext).

Please note that an exception will be raised if the input file is not found or the output file is invalid or not possible as of now. To handle these exceptions, use a try-except block.

This is effectively a file-copying operation in Python. shutil can serve as a useful alternative.




回答5:


First you have to read the file and save it to some variable (here rd_data):

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

Then you have to write the variable to other file:

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

The full code is given below:

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


来源:https://stackoverflow.com/questions/54838127/how-can-i-open-and-read-an-input-file-and-print-it-to-an-output-file-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!