Quick actions

cmd+k|ctrl+k

Navigation

Languages

IO

Snippet info

Language

Python

Visibility

public

Author

projectsfiae

Created

2025-09-03T06:22:38.462942Z

Updated

2025-09-03T06:30:32.258331Z

#read file, this way no need to close reader
with open('test.txt') as my_file:
    print(my_files.readlines())
    
    
#to write we specify second param for open

with open('test.txt', 'r+') as my_file:
    text = my_file.write(':)')
    print(text)
    
# r+ will over write the content, if file not exists throws exception
# w wil just replace whole content, this also creates a new file if file not exists.
#a appends the given content 


#exceeptions
try: 
    with open('test.txt', 'r+') as my_file:
    text = my_file.write(':)')
    print(text)
except FileNotFountError as err:
    print('File does not exist.')
raise err
    
INFO