1

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

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

Comments

0

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)

2 Comments

This only works for reading a single line from the input file and writing it to the output file. Try making this work for the entire content of the file as asked in the question :)
@AshutoshPathak good point! I updated to account for one or more lines depending on the copyFrom file
0

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

3 Comments

You could also write a condition to check whether the output file already exists; if it does, append the contents of the input file to the output file.
Condition is not required, output = open(outputfile,'w+') will do.
Didn't see that, my bad!
0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.