answersLogoWhite

0

How do you read files in python?

User Avatar

Anonymous

11y ago
Updated: 8/20/2019

Assuming that the file contains just textual stuff, create an object to represent it:

stuff = file ( 'stuff.txt' )

To read a line of the file at a time:

line = stuff.readline()

Python signals end-of-file by returning an empty line which you can test for in a construction such as the following:

stuff = file ( 'stuff.txt' )

while True:

line = stuff.readline()

if line :

# do something here

pass

else :

break

You can read the entire file using :

whole_contents = stuff.read()

If the file had contained binary then it would have been necessary to open it as such:

stuff = file ( 'stuff.bn', 'b' )

User Avatar

Wiki User

11y ago

What else can I help you with?

Related Questions

What are read only files?

Files that can only be read (data can be retirieved) are called read-only files. You cannot write any data into a read-only file (you cannot edit it).


What files can cubase read as midi?

...midi files.


Can adobe acrobat read files?

It reads .pdf files.


Can PDF files be read without an Acrobat Reader?

no we can`t read pdf files without Acrobat Reader


What utility you can use to import data from comma-Separated Values files?

You can use utilities like Microsoft Excel, Google Sheets, or programming languages such as Python with libraries like Pandas to import data from comma-separated values (CSV) files. These tools provide options to read and manipulate CSV files efficiently.


Are all microsoftword documents read only files?

Not all Microsoft Word files are read only. A file can be set for read only. By default, MS Word not set to read only.


What is the name of a program that can read postscript files?

The name of a program that can read Postscript files is Postscript Viewer. This program allows the user to view Postscript files in Adobe Reader.?æ


why python is simple?

-It is a high-level and interpreted programming language. -Python is a general-purpose language. -It has a simplified syntax nature. -It is easy to understand. -It is easy to read and write. -Python is much faster than other languages.


Can Kindle read pdf files uploaded to it from a USB memory stick?

The Kindle can read PDF files uploaded from a USB memory stick.


How do you open .npy extension files?

The .npy file extension is opened with the program 'NumPy', a computing and database package for Python.


How do you read files from a unix shl into a procobol program?

You can read the files from Unix school by download the program to a word format. Also, you can use Adobe reader as well to read the material.


Does garage band read .flp files?

No it does not.