<jupyter_start><jupyter_text>File - w : write new file / overwrites if already exists- x : write new file / error if already exists- r : read fiew- a : add file- t : text type - b : binary type for list, object, variables ...  Writing files- text write<jupyter_code>s = """datascience 
fast campus sungsoo"""
f = open("/Users/Seoluca/Data_Analytics/FastCampus/Data_Science_School/1.Programming/file.txt",
        'wt') # 1. find path, 2. define action
f.write(s)
f.close() # always close at the end, and disconnect with the file to save memory capacity<jupyter_output><empty_output><jupyter_text>print<jupyter_code>s = """datascience
fast campus sungsoo
located near sungsoo station"""
f = open("/Users/Seoluca/Data_Analytics/FastCampus/Data_Science_School/1.Programming/file.txt",
        'wt')
print(s, file=f, end="")  # end for saving the lines
f.close()
# overwrite
s = """datascience
fast campus sungsoo
located near sungsoo station x"""
f = open("/Users/Seoluca/Data_Analytics/FastCampus/Data_Science_School/1.Programming/file.txt",
        'wt')
f.write(s)
f.close()<jupyter_output><empty_output><jupyter_text>- binary    - bytes : switches into binary form<jupyter_code>data = bytes(range(0, 10))
f = open("/Users/Seoluca/Data_Analytics/FastCampus/Data_Science_School/1.Programming/binary.b",
        'wb') # write, binary
f.write(data)
f.close()
data<jupyter_output><empty_output><jupyter_text>reading file<jupyter_code># read all
f = open("/Users/Seoluca/Data_Analytics/FastCampus/Data_Science_School/1.Programming/file.txt",
        'rt')
s = f.read()
print(s)
f.close()<jupyter_output>datascience
fast campus sungsoo
located near sungsoo station x<jupyter_text>- reading only part of the lines<jupyter_code>f = open("/Users/Seoluca/Data_Analytics/FastCampus/Data_Science_School/1.Programming/file.txt",
        'rt')
s = f.read(10)  #read only first 10 characters
print(s)
f.close()
f = open("/Users/Seoluca/Data_Analytics/FastCampus/Data_Science_School/1.Programming/file.txt",
        'rt')
s = f.readline()  # read first line
print(s)
f.close()
f = open("/Users/Seoluca/Data_Analytics/FastCampus/Data_Science_School/1.Programming/file.txt",
        'rt')
for _ in range(2):   #read first 2 lines
    s = f.readline()
    print(s, end="")
f.close()
f = open("/Users/Seoluca/Data_Analytics/FastCampus/Data_Science_School/1.Programming/file.txt",
        'rt')
while True:
    s = f.readline()  #read until the end
    if not s:
        break
    print(s, end="")
f.close()
f = f = open("/Users/Seoluca/Data_Analytics/FastCampus/Data_Science_School/1.Programming/file.txt",
        'rt')
s = f.readlines()   #readline's' : print lines in string form
print(s)
f.close()<jupyter_output>['datascience\n', 'fast campus sungsoo\n', 'located near sungsoo station x']<jupyter_text>- binary<jupyter_code>f = open("/Users/Seoluca/Data_Analytics/FastCampus/Data_Science_School/1.Programming/binary.b",
        'rb')  # read, binary
data = f.read()
print(list(data))
f.close()<jupyter_output>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]<jupyter_text>adding files<jupyter_code>s = "\nschool"  # pay attention to "\n" 
f = open("/Users/Seoluca/Data_Analytics/FastCampus/Data_Science_School/1.Programming/file2.txt",
        'at')
f.write(s)
f.close()<jupyter_output><empty_output><jupyter_text>- **with** : close file automatically<jupyter_code>with open("/Users/Seoluca/Data_Analytics/FastCampus/Data_Science_School/1.Programming/file.txt",
        'rt') as f:
    s = f.read()
    print(s)<jupyter_output>datascience
fast campus sungsoo
located near sungsoo station x<jupyter_text>Pickle file Pickle : python module which saves, recover data in binary form through serialization- serialization : saving objects into file- `b`, `t` is not necessary when using Pickle<jupyter_code>import pickle
class A():
    
    def __init__ (self, data):
        self.data = data
    
    def disp(self):      # object which loads function
        print(self.data)
a = A("pickle test")
a.disp()
# save as pickle file
f = open("/Users/Seoluca/Data_Analytics/FastCampus/Data_Science_School/1.Programming/disp.p",
        'wb')  # save as pickle file 'p'
pickle.dump(a, f) 
f.close()
f = open("/Users/Seoluca/Data_Analytics/FastCampus/Data_Science_School/1.Programming/disp.p",
        'rb')  # read binary
load_a = pickle.load(f)
f.close()
load_a.disp()
load_a.data<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13925790196976637, 'fraction_numerical': 0.00847457627118644, 'mean_word_length': 4.424844720496894, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, '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': '645868', 'license': 'MIT', 'n_tokens_mistral': 1676, 'n_tokens_neox': 1581, 'n_words': 337, 'path': '1_python/fastcampus_dss_lectures/07_file.ipynb', 'repo_name': 'lucaseo/TIL', 'size': '4366'}