Skip to content
Snippets Groups Projects
Commit e8ebd74f authored by msyamkumar's avatar msyamkumar
Browse files

Lec 27 files

parent 17ac5b3d
No related branches found
No related tags found
No related merge requests found
Showing
with 1250 additions and 6963 deletions
%% Cell type:markdown id: tags:
# Files and directories
%% Cell type:code id: tags:
``` python
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))
```
%% Cell type:code id: tags:
``` python
# import statements
import os
```
%% Cell type:markdown id: tags:
### Review 1: What does sorted() return?
%% Cell type:code id: tags:
``` python
d = {"Andy": [850, 955], "Meena": [1100, 1320], "Peyman": ["online"]}
t = (45, 32, 29)
sentence = "Meet me at the Sett"
my_list = sentence.split(" ")
# Uncomment each line and observe the types
#print(type(sorted(d)))
#print(type(sorted(t)))
#print(type(sorted(sentence)))
#print(type(sorted(my_list)))
```
%% Cell type:markdown id: tags:
### Review 2: Does sorted return a new object instance or modify the existing object instance?
%% Cell type:code id: tags:
``` python
# sorted returns a brand new object instance, whereas sort() method modifies the existing object instance
```
%% Cell type:markdown id: tags:
### Review 3: Difference between + and append() method on lists
%% Cell type:code id: tags:
``` python
listA = ["Wisconsin", "Madison"]
listB = ["Data" , "Science"]
#print(listA + listB) # + operator creates a brand new object instance
#print(listA[1] + listB[1]) # just like + operator on strings creates a brand new string object instance
# recall that strings are immutable, so you don't have a choice there
#listA.append(listB) # append() method modifies the existing list object instance
#print(listA)
```
%% Cell type:markdown id: tags:
## File processing
- open(...) function call
- file_object.close() function call
%% Cell type:markdown id: tags:
## Reading data from a file
- using read() function call:
- returns file contents as one big string
- convert file object into a list
- each line becomes an item within the list
- works because file objects are iterators
- using for loop to iterate over every line
- works because file objects are iterators
- using next(...) function call to extract a single line
- useful when you want to just process the initial few lines of a file
- ex: extract header line alone from a csv file
%% Cell type:markdown id: tags:
Using read() function call ...
%% Cell type:code id: tags:
``` python
sample_file = open("sample_file.txt")
data = sample_file.read()
sample_file.close()
data
```
%% Cell type:markdown id: tags:
What is the type of return value of read() function?
%% Cell type:code id: tags:
``` python
type(data)
```
%% Cell type:markdown id: tags:
Converting file objects into a list
%% Cell type:code id: tags:
``` python
sample_file = open("sample_file.txt")
data_list = list(sample_file)
sample_file.close()
data_list
```
%% Cell type:markdown id: tags:
What is the type of data_list?
%% Cell type:code id: tags:
``` python
type(data_list)
```
%% Cell type:markdown id: tags:
Using for loop to iterate over file object
%% Cell type:code id: tags:
``` python
sample_file = open("sample_file.txt")
for line in sample_file:
print(line)
print(type(line))
sample_file.close()
data_list
```
%% Cell type:markdown id: tags:
Using next(...) to extract just first item (first line) from file objects
%% Cell type:code id: tags:
``` python
sample_file = open("sample_file.txt")
one_line = next(sample_file)
print(one_line)
print(type(one_line))
sample_file.close()
data_list
```
%% Cell type:markdown id: tags:
### Writing data into a file
- "w" mode in open(...) function call
- BE CAREFUL: every time you invoke open, you will overwrite the file's contents
%% Cell type:code id: tags:
``` python
hello_file = open("hello.txt", "w")
hello_file.write("Hello CS220 / CS319 students.\n")
hello_file.write("Good luck with exam 2 preparation.")
hello_file.write("Ooops forgot newline")
hello_file.close()
```
%% Cell type:markdown id: tags:
Let's read the contents from the file we just wrote
%% Cell type:code id: tags:
``` python
hello_file = open("hello.txt")
data = hello_file.read()
hello_file.close()
data
```
%% Cell type:markdown id: tags:
## os module functions
- os.listdir
- os.mkdir
- os.path.exists
- os.path.isfile
- os.path.isdir
- os.path.join
%% Cell type:code id: tags:
``` python
os.listdir(".")
```
%% Cell type:code id: tags:
``` python
os.mkdir("test_dir")
```
%% Cell type:code id: tags:
``` python
# os.path is a sub-module of os --- does not need importing again
print(os.path.exists("some_file.txt")) #does this file (at this path) exist?
print(os.path.isfile("test_dir")) #nope
print(os.path.isdir("test_dir")) # yes
```
%% Cell type:markdown id: tags:
### os.path.join is a very important function, which enables portability of code
- portability enables you to write code in one OS platform and run it on another OS platform
%% Cell type:code id: tags:
``` python
# this function is like the regular join method, which combines things into a string
# but automatically senses which OS you are using and joins them with either a \ or /
path = os.path.join("test_dir", "file1.txt")
print(path)
# what do you get?
```
%% Cell type:markdown id: tags:
## Exception handling
%% Cell type:code id: tags:
``` python
# let's figure out how to handle a command to open a file that does not exist
path = input("enter the name of the file to open:")
try:
file_object = open(path, "r") # "r" is for reading, but is the default
d = file_object.read()
print(d)
file_object.close()
except FileNotFoundError as e:
print(type(e))
print(path, "could not be opened")
```
%% Cell type:markdown id: tags:
### Python is all about shortening code. Is there a way to shorten the process of:
- opening a file
- handling any Errors while reading/writing
- closing the file
%% Cell type:code id: tags:
``` python
# we can use a 'with' statement to shorten our code
import random
with open("some_numbers.txt", "w") as f:
for i in range(10):
f.write(str(random.randint(1,100)) + "\n")
# don't need to close
# don't need to worry about try/except
```
%% Cell type:markdown id: tags:
### Sum example
%% Cell type:code id: tags:
``` python
#Solution 1: bad solution because we do a lot of reading work before we do any addition (miss the bug)
f = open("nums.txt")
nums = list(f)
f.close()
total = 0
for num in nums:
total += num
print(total)
```
%% Cell type:code id: tags:
``` python
#Solution 2: better solution because start adding immediately after reading numbers from each line
#(catch bugs quickly)
f = open("nums.txt")
total = 0
for num in f:
total += num
print(total)
f.close()
```
%% Cell type:code id: tags:
``` python
#Solution 2: with fix for the bug
f = open("nums.txt")
total = 0
for num in f:
total += int(num)
print(total)
f.close()
```
%% Cell type:markdown id: tags:
### Recursive file search
%% Cell type:code id: tags:
``` python
# program recursive file searcher
import os
def recursiveDirSearch(searchDirectory, searchFileName):
for curr in os.listdir(searchDirectory):
# build a path to this current thing
curr = os.path.join(searchDirectory, curr)
#check if curr is a file
if os.path.isfile(curr):
#check if it contains the search name
if searchFileName in curr: # base case...no recursive call
f = open(curr)
contents = f.read(50) # reads first 50 chars into a string
f.close()
return contents
else: # recursive case!!
contents = recursiveDirSearch(curr, searchFileName)
if contents != None: # we found something
return contents
# finished all recursive searching and never found it
return None
# this function is like our main program
def dir_search(dir_name, file_name):
if not os.path.exists(dir_name):
print("Unable to find searchDirectory!")
else:
contents = recursiveDirSearch(dir_name, file_name)
if contents != None:
print(contents, end = "")
# TODO: figure out how to print "<file_name> not found"
```
Source diff could not be displayed: it is too large. Options to address this: view the blob.
{"meena": 20, "viyan": 30, "rogers":40}
{'meena': 20, 'viyan': 30, 'rogers':40}
{"meena": 20, "viyan": 30, "rogers":40,}
{"meena": false, "viyan": true, "rogers":false}
{"meena": False, "viyan": True, "rogers":False}
{"meena": None, 10: 20, "rogers":10}
This diff is collapsed.
Hello CS220 / CS319 students :)
Hope you are having a wonderful day!
This diff is collapsed.
This diff is collapsed.
hi
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment