# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Index
# - File I/O
# - OS Module

# # File  manipulation

# make a file 
my_file = open('paragraph.txt', 'w')
my_file.write('This is python file\n')
my_file.write('This is python file')
my_file.close()

# open a file
my_file = open('paragraph.txt', 'r')
lines = my_file.readlines()
print('type:', type(lines))
print(lines)
my_file.close()

# +
# best practics for file IO
subjects = ['CSE', 'ETE', 'EEE', 'ME', 'CE']
with open('subject.txt', 'w') as f:
    for subject in subjects:
        f.write(subject + '\n')

print('text from file:')
with open('subject.txt', 'r') as f:
    for line in f:
        print(line)

# exclude new line
print('exlude new line:')
with open('subject.txt', 'r') as f:
    for line in f:
        print(line.strip('\n'))
# -

with open('subject.txt', 'r') as f:
    print('read 1\'st time')
    for line in f:
        print(line.strip('\n'))
    
    print('read 2\'st time')
    for line in f:
        print(line.strip('\n'))

with open('subject.txt', 'r') as f:
    print(type(f))

# # Python Standered Modules 

# ## os

# +
import os

current_working_dir = os.getcwd()
print(current_working_dir)
# -

os.listdir(current_working_dir)

# make some folder using os
folders = ['train', 'test']
for folder in folders:
    os.makedirs(folder)
    print(folder, ' -created.')


# make some folder using os
# if file already exists then what happend ? 
folders = ['train', 'test']
for folder in folders:
    os.makedirs(folder)
    print(folder, ' -created.')

# solution
folders = ['train', 'test']
for folder in folders:
    if not os.path.exists(folder):
        os.makedirs(folder)
        print(folder, ' -created.')
    else:
        print(folder, ' -already exists.')

# +
class_names = ['cat', 'dog']

for _class in class_names:
    train_path = os.path.join('train', _class)
    print(_class)
    print(' ', train_path)
    if not os.path.exists(train_path):
        os.makedirs(train_path)
        print('  creation [ Done ]')

# -

train_cat_absulote_path = os.path.join(os.getcwd(), 'train/cat')
print(train_cat_absulote_path)

# get size of the folder
os.path.getsize(train_cat_absulote_path)

# using shell command
# !du -sh /home/menon/development/path-to-sl/python3_intro/train/cat

import os
# !pwd
# run system command using os 
sys_command = 'pwd'
print(os.system("ls"))

# !ls -ltr

root = 'test_os_walk'
i = 0
for r, d, f in os.walk(root):
    spacing = len(r.split('/')) - 1
    print('\t' * spacing, 'root  :', r)
    print('\t' * spacing, 'folder:', d)
    print('\t' * spacing, 'file  :', f)
    i += 1

# +
# os.remove(path_to_remove)
# os.rename(old_name, new_name)
# -

# ## glob
#   - Todo 

# ## shutil

# +
import shutil

# shutil.rmtree(path/to/dir)
# shutil.copy(src, des)
# shutil.move(src, des)
# -

# # Try Catch Block

def calculate():
    data = [1, 4, 5, 0, 10, 500]
    n = len(data)
    for i in range(n):
        print('{} / {} = {}'.format(i, data[i], i / data[i]))
calculate()


def calculate():
    data = [1, 4, 5, 0, 10, 500]
    n = len(data)
    for i in range(n):
        try:
            print('{} / {} = {}'.format(i, data[i], i / data[i]))
        except Exception as e:
            print('Exception is:', e)
calculate()

# ### Exceptions in Python
#  -https://docs.python.org/3/library/exceptions.html#concrete-exceptions



StarcoderdataJupyterScriptsDedupFiltered