I'm pretty new to programming and I'm currently learning about the open() function used in python. My current goal is to create a to-do list, my first task was to create a filename in read mode otherwise if the file doesn't exist it is to be created in read/write mode "w+".
My current issue is the open() function isn't creating that empty "list.txt" which is the same folder where my code is saved into. I appreciate any feedback :)
def get_list(filename):
try:
f = open(filename, 'r') # try to open the file in read mode
except: # if the file doesn't exist...
f = open(filename, 'w+') # create it by opening it in write
data = f.readlines() # read the content of the file into a list
f.close() # close the file
return data # return data
exceptblock, try printing the exception. Maybe your python application doesn't have the permissions to create files on that folder.get_listfunction returnsNone, not a file object. Indentation is important in python.ls -lto list all file and folder permissions, and usechownandchmodto alter any user / group permissions. If you're using Windows you can see which users are allowed to edit the current folder by clicking on it with the Right Mouse Button and clicking on the Properties option. Running the python script with sudo / admin privilege should fix the problem though.