|
| 1 | +## First option to work with file, |
| 2 | +# open file r - read, a - append, w - write to file , r+ - both read/write |
| 3 | +# need close() expression, files descriptor etc. |
| 4 | +# f = open('test.txt','r') |
| 5 | +# print(f.name) |
| 6 | +# print(f.mode) |
| 7 | +# f.close() # possible leak for max file descriptors in system, Don't Leak file discriptor |
| 8 | + |
| 9 | + |
| 10 | +## Context manager explanation auto close ---> Good Practice!!! |
| 11 | +# with open('test.txt','r') as f: |
| 12 | +# pass |
| 13 | +# print(f.closed) |
| 14 | +# print(f.read()) |
| 15 | + |
| 16 | +## read and print whole file : |
| 17 | +# with open('test.txt','r') as f: |
| 18 | +# f_contents = f.read() |
| 19 | +# print(f_contents) |
| 20 | + |
| 21 | +## read and print line by line file: |
| 22 | +# with open('test.txt','r') as f: |
| 23 | +# with open('test.txt','r') as f: |
| 24 | +# # f_contents = f.readlines() |
| 25 | +# f_contents = f.readlines() |
| 26 | +# print(f_contents) |
| 27 | + |
| 28 | +## read and print single line, first one every time we call function readline will read next line (copy and show) |
| 29 | +# with open('test.txt','r') as f: |
| 30 | +# f_contents = f.readline() |
| 31 | +# print(f_contents) # add end='' |
| 32 | +# f_contents = f.readline() |
| 33 | +# print(f_contents) |
| 34 | + |
| 35 | +## Read extreamly long file: if we read at once, we can run out of memory |
| 36 | +# with open('test.txt','r') as f: |
| 37 | +# for line in f: |
| 38 | +# print(line, end='') |
| 39 | + |
| 40 | +## More control for reading , explanation of read buffer |
| 41 | +# with open('test.txt','r') as f: |
| 42 | +# f_contents = f.read() |
| 43 | + # f_contents = f.read(100) # copy and explain |
| 44 | + # print(f_contents, end='') |
| 45 | + |
| 46 | +## More contr eol for big files with while and EOF condition in while loop, also f.tell() and f.seek() |
| 47 | +# with open('test.txt','r') as f: |
| 48 | +# size_to_read = 10 |
| 49 | +# f_contents = f.read(size_to_read) |
| 50 | + |
| 51 | +# print(f_contents) |
| 52 | +# print(f.tell()) # tell curent position in the file ,show f.seek(0) |
| 53 | +# f_contents = f.read(size_to_read) |
| 54 | +# print(f_contents) |
| 55 | + |
| 56 | + # while len(f_contents) > 0: |
| 57 | + # print(f_contents, end='*') |
| 58 | + # f_contents = f.read(size_to_read) # without it infinite loop, by the end of the file it will return epmty string |
| 59 | + # and hit our while conditional |
| 60 | + |
| 61 | +## Write to file, w flag.. let's create new file (use a flag to append) |
| 62 | +# with open('test2.txt', 'w') as f: |
| 63 | +# f.write('Test') # double it and show f.seek() not common usefull in write mode (show with one letter R) |
| 64 | + |
| 65 | +## Copy file or work with both read/write |
| 66 | +# with open('test.txt', 'r') as rf, open('text_copy.txt', 'w') as wf: # in one line |
| 67 | + |
| 68 | +# with open('test.txt', 'r') as rf: |
| 69 | +# with open('text_copy.txt', 'w') as wf: |
| 70 | +# for line in rf: |
| 71 | +# wf.write(line) |
| 72 | +##### |
| 73 | +### |
| 74 | +## module os introduction |
| 75 | +# import os |
| 76 | + |
| 77 | +# print(os.getcwd()) |
| 78 | + |
| 79 | +# os.system('dir') # Execute shell command |
| 80 | + |
| 81 | +## Make folder |
| 82 | +# os.makedirs('my_dir') |
| 83 | + |
| 84 | +## Print environment variable |
| 85 | +# print(os.environ) |
| 86 | + |
| 87 | +## Make intermediate folders: |
| 88 | +# suppose you want to make a subfolder under your home folder: $HOMEPATH/python-project/project1/temp |
| 89 | +# folder_name = os.path.join(os.environ['HOMEPATH'],'python-project','project1','temp') |
| 90 | +# os.makedirs(folder_name) |
| 91 | + |
| 92 | +## Walking throught directories and files : |
| 93 | +# for dirpath, dirname, filename in os.walk(os.environ['HOMEPATH']): |
| 94 | +# print('Current path',dirpath) |
| 95 | +# print('Directories:', dirname) |
| 96 | +# print('Files:', filename) |
| 97 | +# print() |
| 98 | + |
| 99 | +## files stats |
| 100 | +# print(os.stat('test.txt')) |
| 101 | +# print(os.stat('test.txt').st_size) |
| 102 | +# print(os.stat('test.txt').st_mtime) |
| 103 | + |
| 104 | +# from datetime import datetime |
| 105 | +# mod_time = os.stat('test.txt').st_mtime |
| 106 | +# print(datetime.fromtimestamp(mod_time)) |
| 107 | +# mod_time = os.stat('test.txt').st_mtime |
| 108 | +# print(datetime.fromtimestamp(mod_time)) |
| 109 | + |
| 110 | +## some usefull os.path : |
| 111 | +# print(os.path.basename(os.getcwd())) |
| 112 | +# print(os.path.dirname(os.getcwd())) # only dir name |
| 113 | +# print(os.path.split(os.getcwd())) |
| 114 | +# print(os.path.exists(os.getcwd())) |
| 115 | +# print(os.path.isdir('/Users')) |
0 commit comments