0% found this document useful (0 votes)
1K views2 pages

Python Notes

This document discusses input and output (I/O) in Python. It explains how to write and read strings to/from files, noting that other data types must be converted to strings first. Examples are given to open a file in write and read modes, write a string to the file, read the string back, and iterate over each line of the file. Finally, the common file modes of read-only, write-only, append, read/write, and binary are defined.

Uploaded by

Physicist Manoj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views2 pages

Python Notes

This document discusses input and output (I/O) in Python. It explains how to write and read strings to/from files, noting that other data types must be converted to strings first. Examples are given to open a file in write and read modes, write a string to the file, read the string back, and iterate over each line of the file. Finally, the common file modes of read-only, write-only, append, read/write, and binary are defined.

Uploaded by

Physicist Manoj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

11/16/2014

1.2.6. Input and Output Scipy lecture notes

1.2.6. Input and Output


To be exhaustive, here are some information about input and output in Python. Since we will
use the Numpy methods to read and write files, you may skip this chapter at first
reading.
We write or read strings to/from files (other types must be converted to strings). To write in
a file:
>>> f = open('workfile', 'w') # opens the workfile file
>>> type(f)
<type 'file'>
>>> f.write('This is a test \nand another test')
>>> f.close()

>>>

To read from a file


In [1]: f = open('workfile', 'r')
In [2]: s = f.read()
In [3]: print(s)
This is a test
and another test
In [4]: f.close()
For more details: http://docs.python.org/tutorial/inputoutput.html

1.2.6.1. Iterating over a file


In [6]: f = open('workfile', 'r')
In [7]: for line in f:
...:
print line
...:
This is a test
and another test
In [8]: f.close()

http://scipy-lectures.github.io/intro/language/io.html

1/2

11/16/2014

1.2.6. Input and Output Scipy lecture notes

1.2.6.1.1. File modes


Read-only: r
Write-only: w
Note: Create a new file or overwrite existing file.
Append a file: a
Read and Write: r+
Binary mode: b
Note: Use for binary files, especially on Windows.

http://scipy-lectures.github.io/intro/language/io.html

2/2

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy