Created
November 8, 2020 15:42
-
-
Save twr14152/56d4338035f23342ce7c9023b85170c8 to your computer and use it in GitHub Desktop.
Writing to file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Comparing write method to print method to writing to file. | |
def main(): | |
fname = input("Enter filename: ") | |
dfname = input("Destination Filename: ") | |
infile = open(fname, "r") | |
with open(dfname, "a") as f: | |
data = infile.read() | |
infile.close() | |
f.write("This is a new line number 0\n\n") | |
f.write(data) | |
f.closed | |
main() | |
''' | |
In [41]: ls -l | |
total 16 | |
-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 | |
-rw-r--r-- 1 pi pi 79 Nov 8 10:35 test005.txt | |
In [42]: main() | |
Enter filename: test001.txt | |
Destination Filename: test004.txt | |
In [43]: cat test004.txt | |
This is a new line number 0 | |
This is a test file. | |
line 2 | |
line 3 | |
line 4 | |
end | |
In [44]: | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment