Skip to main content

/docs/images/banner.png

Python

Copy file content

Append content to file and print the contents of the file

# Create a Python script that copies the content of one file to another file.
# copy from A.txt to B.txt
fileA = 'A.txt'
fileB = 'B.txt'

with open(fileA, 'r') as file:
content = file.read()

with open(fileB, 'w') as file:
file.write(content)

with open(fileB, 'r') as file:
print(file.read())

note

To compile and run the program, you can use the following commands:

python3 foo.py
note

files A.txt and B.txt should be in the same directory as the script.