# strip example:
>>> line = ' hello there \n'
>>> line.strip()
'hello there'
Python Guide: File Read/Write
We'll switch over to the guide page - look at the diagrams in there.
with open('file.txt) as f:
for line in f:
# line str from file
File loop, most common, uses least memory, one line at a time
for line in f:
# process each line
line = line.strip() # often do this
In loop, line has '\n' at end. Use line.strip() - return string without whitespace at ends.
Expert extra: in reality there are three line endings found in the wild: '\n', '\r\n', '\r'. Python tries to paper over this difference, so lines all appear to have '\n' as they come in.
crazycat.py - look at print_file_plain()
Try it out with files, see what .strip() does, time it
See PyCharm feature to run Doctests - look at crazy_str() function and test it. See how works in print__file_crazy.
Note: no global variables - use parameters (and Python is not copying big structures).
Demo the > program output capture feature from command line.
Open interpreter >>> in crazycat folder. Type file commands there to start.
s = f.read() # read whole file as a string lines = f.readlines() # read whole file as list of line strs
Python Guide: Command Line
You should be able to write a python script that reads some inputs from the command line. Here we will do a simple example.
When a program runs like this (See the "Python Command" guide at top of this page)
The command line args are the words after the foo.py on the command line:
$ python3 foo.py aaa bbb ccc
1. The program by convention begins running in the function mainn()
2. The first line in main() sets up the variable args to point to a list of the command line args, in this case
['aaa', 'bbb', 'ccc']
def main():
args = sys.argv[1:] # Our standard first line
# now args is ['aaa', 'bbb', 'ccc']
Therefore, you can write code in main() to look at the args and then call different functions. Here is an example problem for us all to do.
How it works: The list sys.argv is just a list like this ['foo.py', 'aaa', 'bbb', 'ccc']. The first element in the list is the python program itself. Trim off that first element with a slice, and we have args, just the rest of the arguments.
Modify the code in crazycat.py to print out the command line args and exit.
def main():
args = sys.argv[1:] # this is stock 1st line
print('args', args)
return
1. experiment: run with command linea: "aaa bbb ccc", "poem.txt", "-crazy poem.txt"
2. Look at main() .. see how it keys off args to call the right python function.
Want:
crazycat.py poem.txt -> print_file_plain('poem.txt')
crazycat.py -crazy poem.txt -> print_file_crazy('poem.txt')
Here is the logic in main to look at command line and call the right function:
if len(args) == 1:
# args[0] is filename
print_file_plain(args[0])
if len(args) == 2 and args[0] == '-crazy':
print_file_crazy(args[1])
d = {} # empty dict
d['a'] = 'alpha' # set
d['g'] = 'gamma'
d['b'] = 'beta'
d['a'] -> 'alpha' # retrieve
'a' in d -> True # "in" key check
>>> d = {}
>>> d['a'] = 'apple'
>>> d['g'] = 'grape'
>>> d['d'] = 'donut'
>>>
>>> d # literal syntax - r/w
{'a': 'apple', 'g': 'grape', 'd': 'donut'}
>>>
>>> d['a']
'apple'
>>>
>>> 'a' in d # "in" efficient
True
>>> 'x' in d
False
>>> not 'x' in d # NO: not this way
True
>>> 'x' not in d # YES: "not in" form preferred
True
>>>
>>> d.keys()
dict_keys(['a', 'g', 'd'])
>>>
>>> # .keys() not a list, but works in loop
>>> for key in d.keys():
... print(key, d[key])
...
a apple
g grape
d donut
>>>
>>> # better: go through keys in sorted order
>>> for key in sorted(d.keys()):
... print(key, d[key])
...
a apple
d donut
g grape
>>>
>>>
>>> d.keys()[0] # Not a list
TypeError: 'dict_keys' object is not subscriptable
>>>
>>> lst = list(d.keys()) # Make list of it
>>> lst[0]
'a'
>>>
We may not get farther than this for lecture-3
counts = {}
for word in xxxxxxx:
if word not in counts:
counts[word] = 0
counts[word] += 1
# Split example - extract parts of a line # a common situation >>> line = '24,37,10/2024\n' >>> line = line.strip() >>> parts = line.split(',') ['24', '37', '10/2024'] >>> parts[0] '24' >>> sum = 0 >>> sum += int(parts[0]) >>> sum 24 >>> >>> # join() is reverse of split() >>> ','.join(['aa', 'bb', 'cc']) 'aa,bb,cc' >>>
Open up in PyCharm and code it up.
1. Two functions TBD read_counts() print_counts
2. See Doctest - can use local file
3. How does data get from read_counts() to print_counts() - see main() This is the right way to do it. Note: doing it without global variables is the best.