Cannot read/write text files with Python "open" function

Hello.


I'm learning about Python from the "HeadFirst Learn to Code Book". Headfirst has a source code named: ('lib.txt'). This is to be read to a file called "read.py" which is in Chap 9 of the book.


To call the function: my_file = open('lib.txt', 'r'). When i do this? i get the error message:


File "/Users/my name/Desktop/Code/Chap 9/read.py", line 2, in <module>

my_file = open('lib.txt', 'r')

FileNotFoundError: [Errno 2] No such file or directory: 'lib.txt'


When i try an "absolute path" or directory i get the same Error Message.


It was suggested that i retrieve the file from the directory of the "Python Script" which is on my Desktop, as well as Applications. I still keep getting the same error message with several different combinations.


Is this due to my Catalina update? or am i doing something wrong here? Because according to the authors on Youtube, this Python "open" function is "super easy".. :(





MacBook Air

Posted on Mar 11, 2020 10:18 AM

Reply
6 replies

Mar 12, 2020 11:49 AM in response to Pythonlearner

#!/usr/bin/env python
# coding: utf-8
import os

# if this is the path where the lib.txt file resides, f will become an absolute path to it
f = os.path.expanduser('~/Desktop/Code/Chap 9/lib.txt')

# with this form of open, the file will automatically close when exiting the code block
with open(f, 'r') as thelib:
    mydata = thelib.read()


In my opinion, it is not a good idea to name Python scripts with names of methods (e.g. read.py), or of functions.


Python versions older than 3.0 assume the script is ASCII, and you probably don't want that, so use the PEP 363 encoding syntax, otherwise explained in Python 2.7.17 documentation section 2.1.4 on encoding declarations.


By default, Catalina installs Python 2.7.16, and if you are using a really old Python book for learning, you may run up against some things that have changed. So, until such time as Apple updates Python on Catalina (and I doubt they will) use the Python 2.7.17 Documentation to re-enforce your Python learning.


Do not use TextEdit or a word processing application for coding. Python is indent sensitive, and you need a programmer's editor that respects those indents. I use the open-ended evaluation of Sublime Text 3, or the paid Textastic from the Mac App Store. Both of these are syntax aware of scores of languages. Sublime has a huge, free contributed package manager site where I have added Python syntax checking (anaconda IDE), and Rubocop support for use within the sublime editor.


Also, do not overlook the included Python IDE (idle) as you can interactively test your code in it and assess results.


idle &


Mar 16, 2020 5:14 AM in response to Pythonlearner

Etresoft may be deep in code at the moment.


The Python open command does not resolve where on your drive that lib.txt file resides. It must know explicitly, so you need to know the tilde, or full path to that text file.


Find the file in the Finder. Right/control-click on that lib.txt file, and when the secondary menu appears, press the option key, and select copy "lib.txt" as Pathname. Then, paste that path into the os.path.expanduser code, as I have shown previously. Now, provided you supply the open command with that path variable, the open command can find your text file.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Cannot read/write text files with Python "open" function

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.