Opening and closing a file

  • open() open a file
  • f = open('somefile.txt')
    • opens file in your current directory for reading
    • file must already exist
  • f = open('/Users/simmonsj/Documents/anotherfile.txt')
    • for a Mac or Linux OS
  • f = open('C:\\Users\\Bob\\information.txt')
    • for Windows
  • Must close the file when finished
    • f.close()

More about opening

  • f = open('myfile', 'r+', encoding = "utf-8")
    • creates and returns a file object f
    • provide file name
    • provide mode
      • r, w, r+, w+, a, r is default
    • provide encoding
      • text mode: meaning read and write strings
      • recommendation: utf-8
      • default is platform dependent

Files and reading

  • each line ends in \n
  • EOF end of file is an empty string ""
  • allcontent = f.read()
    • returns the entire file content as a string
  • f.readlines()
    • returns a list where each line in a file is a list item, \n is include with each line
  • f.readline()
    • file marker
    • returns one line including the \n
file1 = open('myfile.txt', 'r')
Lines = file1.readlines()

count = 0
# Strips the newline character
for line in Lines:
    count += 1
    print("Count and line: ", count, line)
file1.close()
myfile = open("demo.txt", "r")
myline = myfile.readline()
while myline:  # EOF will be ""
    print(myline)
    myline = myfile.readline()
myfile.close()   

Writing to a file

  • open file for writing
    f = open('newfile.txt', 'w')
  • w
    • file is created OR existing is overwritten
  • a
    • if file does not exist, created. If exist, appended to the end
  • r+, w+
    • allows reading and writing of file at the same time
  • must write the \n to get a newline
names = ["Sharon\n", "Kathryn\n", "Sydney\n"]
 
# writing to file
file1 = open('myfile.txt', 'w')
file1.writelines(names)
file1.close()

# Using readline()
myfile = open('myfile.txt', 'r')
myline = myfile.readline()
while myline:
    #print(myline)
    print(myline)
    myline = myfile.readline()
print (type(myline))
if myline == "":
    print("yes")
myfile.close()   
  • Why do you get the blank line between each name?

Output Buffer

  • write() data is buffered before written to a file
  • \n written to the buffer, will push the buffer to the file
  • myfile = open('newfile.txt', 'w', buffering = 50)
    • once 50 bytes are in buffer, will write to file
  • flush()
    • force to flush the output to the file

with statement

  • all statements in with block
  • once out of block, file is automically closed
with open('somefile', 'w') as file:
    file.write('hello world !')

csv files

  • comma separated values or fields
  • reader() returns the entire file read into an object
  • there are also: writerow() and writerows() to write string or strings
import csv

with open('grades.csv', 'r') as myfile:
    grades_reader = csv.reader(myfile, delimiter=',')
    row_num = 1
    for row in grades_reader:
        print(f'Row #{row_num}: {row}')
        row_num += 1

Command-line arguments

  • a python program readsomething.py can be executed from the command line
    >python3 readsomething.py

  • arguments can be passed into the program
    >python3 readsomething.py datafile.txt

  • command-line arguments are stored in sys.argv

    • one string for each argument

sys.argv[0] -> "readsomething.py"
sys.argv[1] -> "datafile.txt"

  • to use sys.argv, import sys

Portability

import os

programs = ['program1.py', 'program2.py', 'program3.py']
file_path = os.path.join('cs149', 'pyprograms', 'hw', 'hw1.1')
for p in programs:
    print(f'file to open: {file_path+p}')

Code from our textbooK:

import sys
import os

if len(sys.argv) != 2:
    
    print(f'Usage: {sys.argv[0]} input_file')
    sys.exit(1)  # 1 indicates error

print(f'Opening file {sys.argv[1]}.')

if not os.path.exists(sys.argv[1]):  # Make sure file exists
    print('File does not exist.')
    sys.exit(1)  # 1 indicates error

f = open(sys.argv[1], 'r')

# Input files should contain two integers on separate lines

print('Reading two integers.')
num1 = int(f.readline())
num2 = int(f.readline())


print(f'Closing file {sys.argv[1]}')
f.close()  # Done with the file, so close it


print(f'\nnum1: {num1}')

print(f'num2: {num2}')

print(f'num1 + num2: {num1 + num2}')


Windows and Posix (Linux,MacOS) filesystem paths

  • what is a path?
  • directory paths are represented differently
  • module pathlib
from pathlib import Path

print(Path.home())
# print current working directory
print(Path.cwd())

p = Path('/Users/simmonsj/Documents/Classes/cs149/programs')
# list of all py files
print(list(p.glob('**/*.py')))
# set back to cwd
p = Path('.')
print(list(p.glob('**/*.py')))

Interesting code

from pathlib import Path

path = Path.cwd() / "shopping_list.md"
with path.open(mode="r", encoding="utf-8") as md_file:
    content = md_file.read()
    # whatever is needed for the contents of the file