Aug 10-14, 2020 Vikrant Patil
These notes are available online at http://notes.pipal.in/2020/arcesium_finop_batch1_module1/day5.html
© Pipal Academy LLP
Day 1 | Day 2 | Day 3 | Day 4 | Day 5
We will be using jupyter hub from http://lab1.pipal.in for this training.
make use of notebook module1-day5.ipynb for today's session.
import os
os.getcwd() # this would return current working directory /folder
os.listdir() # lists files and directories in current folder
os.listdir("/tmp")
os.path.join("/", "home", "vikrant")
os.path.exists("/home/vikrant/")
os.path.getsize("/home/vikrant/Documents/acer.txt")
os.path.join("x","y")
cwd = os.getcwd()
path = os.path.join(cwd, "x", "y")
os.getcwd()
path
path1 = os.path.join(cwd, "test")
path1
os.mkdir(path1)
!ls
os.path.exists(path1)
path2 = os.path.join(cwd, "test1")
path2
!ls
os.path.exists(path2)
filename = "acer.txt"
path = os.path.join("/","home","vikrant","Documents",filename)
path
import os
for path, dirs, files in os.walk(os.getcwd()):
for f in files:
print(os.path.join(path,f))
stocks = {"name":"IBM", "open":123, "high":125, "low":122, "close":123.5}
for item in stocks:
print(item)
for key, value in stocks.items():
print(key, value)
for key in stocks:
print(key, stocks[key])
x, y = 2, 3
list(stocks.items())
for x, y in stocks.items():
print(x, y)
for path, dirs, files in os.walk(os.getcwd()):
for f in files
for x , y , z in os.walk(os.getcwd()):
problems
longlistdir which will print filenames and flolder names from given folder, such that before every file it prints a char f and before every directory it prints a character d >>> longlistdir(folderpath)
d training
f day1.html
f day2.html
d test
f hello.py
findfiles which finds all files in given directory with given extension >>> findfiles("/var", "log")
['/var/log/alternative.log' ....]
dirsize which will find size of dorectory in MB. >>> dirsize(os.getcwd())
1.9
os.listdir()
os.path.isfile(os.path.join(os.getcwd(),"push")) # this checks if given path is file or directory
os.path.isdir(os.path.join(cwd, "test"))
os.path.isfile(os.path.join(cwd, "test"))
def longlistdir(folder): # this is not recursive
files = os.listdir(folder)
for file in files:
path = os.path.join(folder, file)
if os.path.isfile(path):
print("f", path)
else:
print("d", path)
longlistdir(cwd)
def longlistdir_(folder):
for path, dirs, files in os.walk(folder):
for f in files:
print("f", os.path.join(path, f))
for d in dirs:
print("d", os.path.join(path, d))
longlistdir_(cwd)
def print_list(items, path, start="f"):
for item in items:
print(start, os.path.join(path, item))
def longlistdir_(folder):
for path, dirs, files in os.walk(folder):
print_list(files, path)
print_list(dirs, path, start="d")
longlistdir_(cwd)
dir
dir
list
dir_ ="a"
def findfiles(dirpath, ext="log"):
search = []
for path, dirs, files in os.walk(dirpath):
for f in files:
if f.endswith("." + ext):
search.append(f)
return search
findfiles("/home/vikrant/Downloads//", "pdf")
x = 3**100
x
y = 2**100
y
x = 3**100
print(x)
y = 2**100
print(y)
def get_sizes(files, path):
s = 0
for f in files:
filepath = os.path.join(path, f)
s += os.path.getsize(filepath)
return s
def dirsize(folderpath):
s = 0
for path, dirs , files in os.walk(folderpath):
s += get_sizes(files, path)
s += get_sizes(dirs, path)
return s/(1024*1024) # to make in MB
dirsize(cwd)
dirsize("/home/vikrant/Downloads/")
path dirs files
+root root [folder1, folder4] [f3.txt, f4.txt]
|
+--folder1 root/folder1 [folder] [f1.txt, f2.txt]
| |
| +-- f1.txt
| +-- f2.txt
| +-- folder root/folder/folder [] [x.txt]
| |
| +--- x.txt
|
+--f3.txt
+--f4.txt
+--folder4 root/folder4 [] []
%%file stats.py
import math
def mean(nums):
return sum(nums)/len(nums)
def std(nums):
m = mean(nums)
s = 0
for n in nums:
s += (n-m)**2
return math.sqrt(s/(len(nums)-1))
def median(nums):
n = len(nums)
c = n//2
if n%2==0:
return (numc[c]+num[c-1])/2
else:
return num[c]
Your own module is nothing but a text file with extenstion .py. Inside this file you write your releated python functions and data. This modue can be imported directly in python interpreter if the file is in current working directory. If it is not in current folder, then one has to add path of folder in which the file resides, to a system variable to PYTHONPATH.
import stats
stats.mean([1, 2, 3, 4, 5])
stats.std([1, 2, 3,4 ,5 ,6])
stats.median([1, 2, 3, 4])
%%file stats2.py
import math
def mean(nums):
return sum(nums)/len(nums)
def std(nums):
m = mean(nums)
s = 0
for n in nums:
s += (n-m)**2
return math.sqrt(s/(len(nums)-1))
def median(nums):
n = len(nums)
c = n//2
if n%2==0:
return (nums[c]+nums[c-1])/2
else:
return nums[c]
import stats2
stats2.median([1, 2, 3, 4])
%%file hello.py
import sys
def hello(name):
print("Hello", name + "!")
def welcome(name):
hello(name)
print("Welcome to python programming!")
name = sys.argv[1]
welcome(name)
!python3 hello.py vikrant
%%file hello1.py
import sys
def hello(name):
print("Hello", name + "!")
def welcome(name):
hello(name)
print("Welcome to python programming!")
print("sys.argv arguments -> ", sys.argv)
name = sys.argv[1]
welcome(name)
!python3 hello1.py vikrant kfddf saghjg hgshjg khdfjgdsf
!python3 hello1.py vikrant
!python3 hello1.py vikrant 23 1.1 jkhdfj kdjhfkd
%%file add.py
import sys
def add(x, y):
return x+y
a = int(sys.argv[1])
b = int(sys.argv[2])
add(a, b)
!python3 add.py 3 5
%%file add.py
import sys
def add(x, y):
return x+y
a = int(sys.argv[1])
b = int(sys.argv[2])
print(add(a, b))
!python3 add.py 343 4343
import add
%%file add1.py
import sys
def add(x, y):
return x+y
print(__name__)
a = int(sys.argv[1])
b = int(sys.argv[2])
print(add(a, b))
!python3 add1.py 34 23
import add1
%%file add2.py
import sys
def add(x, y):
return x+y
print("__name__ = ", __name__)
if __name__ == "__main__":
a = int(sys.argv[1])
b = int(sys.argv[2])
print(add(a, b))
!python3 add2.py 34 54 # when we run it as script, value of varianble __name__ -> "__main__"
import add2 ## when we import , value of variable __name__ -> module name
def hello():
print("hello")
%%file add3.py
import sys
def add(x, y):
return x+y
print("__name__ = ", __name__)
if __name__ == "__main__":
a = int(sys.argv[1])
b = int(sys.argv[2])
print(add(a, b))
!python3 add3.py 34 54
import add3