<jupyter_start><jupyter_text>Module-6File Handling<jupyter_code>cd /content/drive/My Drive/python_basic/mod_06_filehandling_import
!pwd
!ls
!touch myfile.txt
x = open("myfile.txt")
x.<jupyter_output><_io.TextIOWrapper name='myfile.txt' mode='r' encoding='UTF-8'><jupyter_text>File Mode|Mode | operation    | pointer location       ||---- | ------------------------------------ | ------------||r    | reading mode                          | brown **fox**  ||w    | writing,if not exist create new file  | natural ||x   | Open a new File                         | transparent ||a     |open for appending at the end           ||t     |Open in text mode                       ||b    | open in binary mode<jupyter_code>print(x.read())
x = open("myfile.txt",)
print(x.tell()) #return curent position
print(x.read())
print(x.tell())
x = open('myfile.txt',)
x.seek(12) #seewk current position
print(x.read())
x.close()
x = open("myfile.txt")

print(type(x.read())) #read data in single string format
#read data line by line
#every line a one string and store all string in list
x = open("myfile.txt")
data = x.readlines()
print(data)
x.close()
x = open("myfile.txt")
data = x.readlines()
for p in data:
  print(p)

x.close()
x = open("myfile.txt")
data = x.readlines()
for p in data:
  print(p, end="")

x.close()<jupyter_output><NAME>
ritesh
pankaj
ajit
dilip<jupyter_text>Example<jupyter_code>!touch mydata.txt
cat = {}
catfile = "mydata.txt"
with open(catfile, 'r') as f:
  for line in f:
    ls = line.strip()
    print(ls)
cat = {}
catfile = "mydata.txt"
with open(catfile, 'r') as f:
  for line in f:
    ls = line.strip().split()
    cat[ls[0]] = ls[1]
    print(ls)
type(ls)
type(cat)
cat.get("Bag")<jupyter_output><empty_output><jupyter_text>writing data into the file<jupyter_code>!touch file2.txt
x = open('file2.txt', "w")
x.write("python\njava\n") #remove existing data  and 
x.close()
x = open('file2.txt', "a") #open in append mode
x.write("python\njava\n") #remove existing data  and 
x.close()<jupyter_output><empty_output><jupyter_text>Pickling: convert python object to a file suppported format, and object store in the filealso called seralization<jupyter_code>import pickle
data =[["meraz", 789], ["dilip", 5674]]

x = open('pickle.txt', "wb") #write mode and store in binary format
pickle.dump(data, x)
x.close()<jupyter_output><empty_output><jupyter_text>Unpickling<jupyter_code>import pickle
x = open("pickle.txt", "rb")
data = pickle.load(x)
x.close()

print(data)<jupyter_output>[['meraz', 789], ['dilip', 5674]]<jupyter_text>Comprehension techniqueSingle line statement<jupyter_code>my_list = [0, 1, 2, 3, 4, 5, 6, 7]
print(my_list)
new_list = [var for var in my_list]
print(new_list)
new_list2 = [var for var in my_list if var%2 == 0]
print(new_list2)<jupyter_output>[0, 2, 4, 6]<jupyter_text>---New Example<jupyter_code>users  = [{"name": "meraz", "age":27}, {"name": "Dilip", "age":31}, {"name": "Ritesh", "age":35}]
print(users)
name = [user["name"] for user in users]
print(name)
name2 = [user["name"] for user in users if user["age"] > 30]
print(name2)<jupyter_output>['Dilip', 'Ritesh']<jupyter_text>---Exaple2<jupyter_code>users  = [
          [{"name": "meraz", "age":27},
           {"name": "Dilip", "age":31}, 
           {"name": "Ritesh", "age":35}],
          [{"name": "Sujit", "age":27},
           {"name": "Ajit", "age":22}, 
           {"name": "Aditya", "age":31}]
          ]
print(users)
data = [user["name"] for usert in users for user in usert ]
print(data)
data2 = [user["name"] for usert in users for user in usert if user["age"] > 30]
print(data2)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18570640176600442, 'fraction_numerical': 0.018763796909492272, 'mean_word_length': 3.952185792349727, 'pattern_counts': {'":': 18, '<': 34, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '110578', 'license': 'MIT', 'n_tokens_mistral': 1421, 'n_tokens_neox': 1362, 'n_words': 340, 'path': 'mod_06_filehandling_import/mod_06_02.ipynb', 'repo_name': 'merazlab/python', 'size': '3626'}