Opening and appending to file in Python3

Joined
Mar 29, 2023
Messages
14
Reaction score
1
I am really enjoying myself learning Python3. But now i seem to have hit a brick wall and cannot find a way around it.
I have been practicing the little i know of python3 with a simple little script called "practice.py" as follows:

Code:
    #!/usr/bin/env python3
    #
    practice = ["Billy", "John", "Peter", "Simon", "Kevin"]
    print(*practice, sep='\n')

Which returned the following:

Billy
John
Peter
Simon
Kevin

Flushed with success i decided to add another name to the list "a":

Code:
    f = open("lpthw/practice.py", "a") 
    When i try to go to the second line i recieve an error of "Invalid syntax"

Could some one help me out here please.
 
Joined
Dec 10, 2022
Messages
94
Reaction score
26
This f = open("lpthw/practice.py", "a") doesn't do anything by itself.
I recommend reading about working with files.
 
Joined
Mar 29, 2023
Messages
14
Reaction score
1
I have now got around the error and can't wait to learn all i can regarding the Python programming Language.
I have to agree with VBService that using With Open is a better option. Big thanks for the replies.
 
Joined
Nov 23, 2023
Messages
59
Reaction score
3
I am really enjoying myself learning Python3. But now i seem to have hit a brick wall and cannot find a way around it.
I have been practicing the little i know of python3 with a simple little script called "practice.py" as follows:

Code:
    #!/usr/bin/env python3
    #
    practice = ["Billy", "John", "Peter", "Simon", "Kevin"]
    print(*practice, sep='\n')

Which returned the following:

Billy
John
Peter
Simon
Kevin

Flushed with success i decided to add another name to the list "a":

Code:
    f = open("lpthw/practice.py", "a")
    When i try to go to the second line i recieve an error of "Invalid syntax"

Could some one help me out here please.
  • open("myfile.txt", "a") opens the file "myfile.txt" in append mode.
  • The with statement ensures the file is automatically closed after the indented block finishes, even in case of errors.
  • file.write("This text will be appended to the file.\n") writes the string to the file, followed by a newline character (\n).
 
Joined
Dec 10, 2022
Messages
94
Reaction score
26
Here is a quick example working with a list and a text file

Python:
# Working with list
print('working with list')
practice = ["Billy", "John", "Peter", "Simon", "Kevin"]
print(*practice, sep='\n')
# Add name to list
practice.append('Kelly')
print()
print('updated list')
print(*practice, sep='\n')

print()
print('working with files')
# Working with files
txt_file = 'practice.txt'
print('reading file')
with open(txt_file, 'r') as tfile:
    data = tfile.readlines()
    print(*data, sep='')
print('writing to files')
with open(txt_file, 'a') as tfile:
    tfile.write('Jude\n')
print('reading updated file')
with open(txt_file, 'r') as tfile:
    print(*tfile.readlines(), sep='')

Output
Code:
working with list
Billy
John
Peter
Simon
Kevin

updated list
Billy
John
Peter
Simon
Kevin
Kelly

working with files
reading file
Billy
John
Peter
Simon
Kevin

writing to files
reading updated file
Billy
John
Peter
Simon
Kevin
Jude
 
Joined
Nov 23, 2023
Messages
59
Reaction score
3
I am really enjoying myself learning Python3. But now i seem to have hit a brick wall and cannot find a way around it.
I have been practicing the little i know of python3 with a simple little script called "practice.py" as follows:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,795
Latest member
YolandaCar

Latest Threads

Top