How to open a text file from my desktop while using python 3.7.1 in Terminal
I saved a text file to my desktop named "test.txt" within the file I wrote only my name, David. Then, I opened terminal and opened python 3.7.1 and wrote the following code in attempt to see my name, David, populate:
However, I receive the following error message:
SyntaxError: EOL while scanning string literal
Does anyone know how I can avoid this error and have my name, David, read from the test.txt file on my desktop? Or am I going about this completely wrong?
As @Matt explained, you are missing quotes.
You can follow below approach to open file and read from it.
myfile = open("/Users/David/Desktop/test.txt","r") #returns file handle myfile.read() # reading from the file myfile.close() # closing the file handle, to release the resources.
Opening files and reading from files, What is the proper way to open a file that you intend to read from? To open a file in Python, we first need some way to associate the file on disk with a variable in Python. This process is called opening a file. We begin by telling Python where the file is. The location of your file is often referred to as the file path. In order for Python to open your file, it requires the path.
You are missing a quotation mark, after your file path. It should look like this:
open("/Users/David/Desktop/test.txt","r") ^ This quotation mark
This will open the file correctly, however you will still need to actually read from it.
How To Read and Write Files in Python 3, How to opening files and read from files and avoid annoying mistakes when reading files. Here's the official Python documentation on reading and writing from files. But before reading Pretend you have a file named example.txt in the current directory. In your system shell (i.e. Terminal), change to your Desktop folder: File Handling. The key function for working with files in Python is the open() function. The open() function takes two parameters; filename, and mode. There are four different methods (modes) for opening a file:
You are missing the other quotations as the others have mentioned. Try using the
with open statement, as it handles your resources for you, meaning you don't need to specify
with open("/Users/David/Desktop/test.txt", "r") as file: file.read()
3. Using Python on Windows, After a brief introduction to file formats, we'll go through how to open, read, and as well as a local programming environment set up on your computer. We will use the open() function to open the days.txt file and assign it to the After saving your code, open up terminal and run your Python script, like so:. File Handling File handling in Python requires no importing of modules. File Object Instead we can use the built-in object "file". That object provides basic functions and methods necessary to manipulate files by default. Before you can read, append or write to a file, you will first have to it using Python's built-in open() function.
3. Using Python on Windows, After starting the installer, one of two options may be selected: This allows the open() function, the os module and most other path Python uses it for the default encoding of text files (e.g. locale.getpreferredencoding() ). what versions are installed on the PC and without regard to the order in which they were installed 1.1.1. Interface options¶. The interpreter interface resembles that of the UNIX shell, but provides some additional methods of invocation: When called with standard input connected to a tty device, it prompts for commands and executes them until an EOF (an end-of-file character, you can produce that with Ctrl-D on UNIX or Ctrl-Z, Enter on Windows) is read.
1.9. The Idle Editor and Execution, Then, open the Advanced tab and click the Environment Variables button. In short, your path is: My Computer ‣ Properties ‣ Advanced ‣ Environment Variables This executable opens a terminal, which stays open even if the program This means that when you double-click on one of these files from Try using the code on your computer first with the desktop version of Python before trying it in MicroPython. In a terminal navigate to the same directory as the test.py file (this is very important, you must be in the same directory as test.py!) and run the python3 command (or python if using Python 2.x). At the Python REPL enter the following
Getting started, That is most easily done by creating a text file and running the Python interpreter on the file. If you already had a file open, and its window has the foreground focus when Switch to your Desktop or Documents, and find the right folder from there. terminal window created when you run a Python program from the Finder. Either you can use a 3rd party python module like xlrd, or save your excel file a CSV file, instead of a normal Excel file. I think the point you are missing is that an excel file has no resemblance to a plain text file. Open the Excel document in notepad and you will see what I mean.