t1 == t2 t1 is t2 ############ Writing Modules ################# def linecount(filename): count = 0 for line in open(filename): count += 1 return count print(linecount('Classwork/Session 13/Exercises/wc.py')) #any file contains python can be imported as a module #this program reads itself and prints the number of lines in a file which is 7 import wc print(wc) wc.linecount('Classwork/Session 13/Exercises/wc.py') if __name__ == '__main__': print(linecount('Classwork/Session 13/Exercises/wc.py')) # __name__ = built in variable that is set when the program starts and has the value #__main__ ; the test code runs otherwise if module is being imported, the test code #is skipped.
import wc print(wc) print(wc.linecount('example3.py'))
#### section 14.7 # import pickle # t1=[1,2,3] # s=pickle.dumps(t1) # t2=pickle.loads(s) # print(pickle.dumps(t1)) # print(t2) # print(t1==t2) # print(t1 is t2) #### train 14.3 -> skip #### section 14.8 # skip #### train 14.4 -> skip #### section 14.9 import wc print(wc.linecount('wc.py')) #### train 14.5 -> skip #### section 14.10 # nothing #### section 14.11 # nothing #### section 14.12 #### train 14.5(typo 14.6?) -> skip
import wc print wc.linecount('wc.py')
import os import wc import hashlib ''' cmd = 'ls -1' fp = os.popen(cmd) res = fp.read() stat = fp.close() print(stat) ''' filename = 'output.txt' cmd = 'md5sum ' + filename fp = os.popen(cmd) res = fp.read() stat = fp.close() print(res) print(stat) print('hello') wc.linecount('chap_14_8.py')
cmd = 'md5sum ' + to_chk_md5 print(cmd) fp = os.popen(cmd) res = fp.read() stat = fp.close() print(res) print(stat) #/// WRITING MODULES print('\n\n/////WRITING MODULES/////\n') import wc print(wc) print(__name__) print(wc.linecount('ian_notes_files.py')) #/// DEBUGGING print('\n\n/////DEBUGGING/////\n') #finding whitespaces for debugging s = '1 2\t 3\n 4' print(s) print(repr(s))
cmd = 'dir' fp = os.popen(cmd) res = fp.read() print(res) stat = fp.close() print(stat) def open_zip(filename): cmd = 'gunzip -c ' + filename fp = os.popen(cmd) return fp print(wc) print(wc.linecount('output.txt')) print(__name__) print(wc.__name__) s = '1 2\t 3\n 4' print(s) print(repr(s)) e = [] fin = open('output.txt') for line in fin: words = line.strip() w = words.split() print(w)
import matplotlib.pyplot as plt2 import matplotlib.pyplot as plt3 plt1.hist(coluna1,1,'red') plt1.xlabel("Idade(anos)") plt1.ylabel("Frequencia") plt1.show() plt2.hist(coluna2,0.05,'red') plt2.xlabel("Altura(m)") plt2.ylabel("Frequencia") plt2.show() plt3.hist(coluna3,2,'red') plt3.xlabel("Peso(kg)") plt3.ylabel("Frequencia") plt3.show() ''' print("Exercicio 2") import os for root, dirs, files in os.walk(".", topdown=False): for name in files: print(os.path.join(root, name)) for name in dirs: print(os.path.join(root, name)) print("Exercicio 3") import wc wc.linecount('dados_alunos.txt')
t is t2 ## writing modules # wc.py def linecount(filename): count = 0 for line in open(filename): count += 1 return count import wc print(wc) wc.linecount('wc.py') ## delimiated file # tab delimited import csv def process(date, symbol, price): print(date, symbol, price) with open( 'D:/Dropbox/�곗씠�� 諛깆뾽/Lecture/2018�� 1�숆린 怨좉툒�듦퀎�꾨줈洹몃옒諛�/肄붾뱶/tab_delimited_stock_prices.txt', 'r', encoding='utf8',
import wc from wc import __name__ print(__name__) print(wc.linecount('escrevendo módulos.py'))
fout.write(path) else: walk(path) #walk ("/home/aadhith/Python") #Handling an exception with a try statement is called catching an exception try: fin = open('bad_file') except: print('Something went wrong.') #pipes concept not clear """cmd = "ls -l" fp = os.popen (cmd) print (fp) #this is an object res = fp.read (fp) print (res)""" #def linecount (filename): """ count = 0 for line in open (filename): count += 1 return count print (linecount ("wc.py"))""" import wc print(wc.linecount("wc.py"))
# for filename in files: # print(os.path.join(root, filename)) # try: # fin = open('bad_file') # except: # print('Something went wrong.') # # The pickle module can translate almost any type of object into a string # # suitable for storage in a database, and then translates strings back into objects. # import pickle # t = [1, 2, 3] # print(pickle.dumps(t)) # with open('session14/saved.p','wb') as p: # pickle.dump(t, p) # t1 = [1, 2, 3] # s = pickle.dumps(t1) # t2 = pickle.loads(s) # print(t2) # print(t1 == t2) # print(t1 is t2) import wc print(wc) print(wc.linecount('session14/result.txt'))
for line in fin: line = line.replace(pattern, replacestr) fout.write(line) fin.close() fout.close() except: print "Something went wrong" # sed('gif', 'pdf', 'chap14in.txt', 'chap14out.txt') cmdCopy = 'cp book0.txt /Users/Steven/Desktop' fp = subprocess.call(cmdCopy, shell=True) cmdRemove = 'rm /Users/Steven/Desktop/book0.txt' fp = subprocess.call(cmdRemove, shell=True) print wc print wc.linecount('chapter14.py'), 'lines' # conn = urllib.urlopen('http://thinkpython.com/secret.html') conn = urllib.urlopen('https://docs.python.org/2/library/urllib.html') fout = open('parsedoutput.txt', 'w') for line in conn: line = line.strip(string.punctuation+string.whitespace) # if 'Example:' in line: fout.write(line) fout.write("\n") fout.close()