Working with files in Python
Files in Python
slide 1
What You Need In Order To Read Information From A File
1. Open the file and associate the file with a file variable. 2. A command to read the information. 3. A command to close the file.
slide 2
1. Opening Files
Prepares the file for reading:
A. Links the file variable with the physical file (references to the file variable are references to the physical file).
B. Positions the file pointer at the start of the file.
Format:1
= open(, "r")
Example: (Constant file name)
inputFile = open("data.txt", "r") OR
(Variable file name: entered by user at runtime)
filename = input("Enter name of input file: ") inputFile = open(filename, "r")
s1lidEe x3amples assume that the file is in the same directory/folder as the Python program.
B. Positioning The File Pointer
letters.txt A
B
C
B
B :
slide 4
2. Reading Information From Files
? Typically reading is done within the body of a loop ? Each execution of the loop will read a line from the file into a
string
Format:
for in :
Example:
for line in inputFile: print(line) # Echo file contents back onscreen
slide 5
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- data files in python
- writing your first python program
- reading and writing data files with python
- reading and writing vector data with ogr
- working with files in python
- c h a p r 2 file handling in python
- python programming 1 variables loops and
- basic python programming for loops and reading
- pyroot tutorial python
Related searches
- working with time in tableau
- working with columns in word
- python working with dataframes
- python working with csv files
- working with strings in vba
- working with data in excel
- working with tables in excel
- working with arrays in excel
- working with csv in python
- clearances for working with children in pa
- working with files in python
- working with json in python