Skip to content

Instantly share code, notes, and snippets.

@twr14152
Last active November 8, 2020 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twr14152/f2f4da0158a5adc862baffddf51e9df2 to your computer and use it in GitHub Desktop.
Save twr14152/f2f4da0158a5adc862baffddf51e9df2 to your computer and use it in GitHub Desktop.
Write to file using print
#Trying to understand the benefit of this method of writing to a file...
def main():
fname = input("Enter filename: ")
dfname = input("Destination Filename: ")
infile = open(fname, "r")
outfile = open(dfname, "a")
data = infile.read()
print(data)
infile.close()
print(data, file=outfile)
print("This is a new line\n\n", file=outfile)
outfile.close()
'''
In [24]: cat test001.txt
This is a test file.
line 2
line 3
line 4
end
In [20]: main()
Enter filename: test001.txt
Destination Filename: test003.txt
This is a test file.
line 2
line 3
line 4
end
In [21]: ls -l
total 12
-rw-r--r-- 1 pi pi 50 Nov 8 09:54 test001.txt
-rw-r--r-- 1 pi pi 51 Nov 8 10:07 test002.txt
-rw-r--r-- 1 pi pi 72 Nov 8 10:11 test003.txt
In [22]: cat test003.txt
This is a test file.
line 2
line 3
line 4
end
This is a new line
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment