File Write in Python

·

6 min read

How to write files in python

open('path', 'mode', encoding='UTF-8')

Above is the fundemental method for opening files in different locations using python. The path can be categorized into two major means:

Relative paths and Absolute paths

Relative paths are more easier to use, but you need to start from where your codes are from (if you're writing your code in a file called python, you need to write the path starting from the file python) on the other hand absolute paths start from the very beginning, like the terminal (C:\EricShin\example...).

The old way

f = open('./resource/it_news.txt', 'r', encoding='UTF-8')
# checking directory of the file
print(dir(f))
# checking the encoding (in this case we encoded as UTF-8, so it will print UTF-8)
print(f.encoding)
# file name
print(f.name)
# mode
print(f.mode)
cts = f.read()
print(cts)
# and you HAVE TO CLOSE FOR THIS KIND OF METHODS
f.close()

Now, there are two things here that you should be wary of: mode and close. There are basically five modes in opening txt files, r, w, a, t, and b. r is used to read, w to write, a to append, t to text, and b to binary. We'll talk about what each mode does later. The close part, f.close(), is crucial to coding filewrite in python this way. Since we declared f as opening it_news.txt with read mode, we have to close it to 'undeclare' it so it doesn't get in our way later.

With

Like you saw above, declaring open methods and closing is too much. So there is a simple way of doing this: using the with function.

with open('./resource/it_news.txt', 'r', encoding='UTF-8') as f:
    c = f.read()
    print(c)
    print(iter(c))
    print(list(c)) # prints a list of each letter divided

Here, you won't need to close the declaration, as the f being open('./resource/it_news.txt', 'r', encoding='UTF-8') only is available within the with statement. Outside, you can redefine f as anything you want. I prefer using with, and a lot of people do. Try to get used to this method if you can.

read(20) and seek(0,0)

with open('./resource/it_news.txt', 'r', encoding='UTF-8') as f:
    c = f.read(20)
    print(c)
    c = f.read(20)
    print(c)
    c = f.read(20)
    print(c)
    f.seek(0,0)
    c = f.read(20)
    print(c)

.read(number) reads the number of words starting from where your 'cursor' would be. if you read 20 bytes before, then the cursor starts from the 21st byte. Then here you will probably be able to infer that seek is moving the cursor to a specific place. You're right! That's exactly what it does.

readline

with open('./resource/it_news.txt', 'r', encoding='UTF-8') as f:
    line = f.readline()
    print(line)
    line = f.readline()
    print(line)

This works similarly to the .read(number), but the difference is that you only read a line at a time. and the cursor this time of course moves too.

readlines

with open('./resource/it_news.txt', 'r', encoding='UTF-8') as f:
    cts = f.readlines()
    print(cts)
    print()
    for c in cts:
        print(c, end='')

readlines stores each line into a variable in a list. so for the print(cts) part, it will print a very long list consisting of lines as variables with '\n's at the end.

w and a mode

So far we only dived into what the r mode can do. However, the most commonly used mode is the w and a. w and a are similar but slightly different. Take this for an example:

with open('./resource/contents1.txt', 'w') as f:
    f.write('I love python\n')

with open('./resource/contents1.txt', 'a') as f:
    f.write('I love python2\n')

So there are TWO with statements. it does the exact same thing, but the difference is that the modes of the with open functions are different. WHY A IN THE LATTER ONE? It's simple: using two separate with statements with the same modes and with the same statements, confuses the computer, so if the second with statement's mode was w, it would only print one l love python. However, setting the mode of the second with a statement as a(append) would literally append the file and this would ultimately print two I love python in a file called contents1.txt.

writelines

with open('./resource/contents2.txt', 'w') as f:
    list = ['Orange\n', 'Apple\n', 'Banana\n', 'Melon\n']
    f.writelines(list)

List -> Files

So for the case above, it will write this :

Orange

Apple

Banana

Melon

Onto the contents2.txt file

print

with open('./resource/contents3.txt', 'w') as f:
    print('Test Text Write!', file=f)
    print('Test Text Write!', file=f)
    print('Test Text Write!', file=f)

This is a way of using the print function. So here, as we declared opening the contents3.txt file as f, the print function with file=f will print the method directly onto the file.

csv files

csv files are widely used in recognizing data and sorting them. In python, you can directly create and manipulate csv easily.

Forms of csv files

The top of the csv file is the key. It represents which data is containing which. For example, if the very top of a csv file was Name,Code, the first variable of every other lines of this csv file would represent a name, and a second variable the code.

Name,Code Afghanistan,AF Aland Islands,AX Albania,AL Algeria,DZ American Samoa,AS Andorra,AD

It would look like this and will go on and on. However, commas aren't the only type for distribution. You can use | this symbol as well.

You first need to import csv

import csv
with open('./resource/test1.csv', 'r') as f:
    reader = csv.reader(f)
    # next(reader) Header Skip (this skips the header)
    # checking object
    print(reader)
    # checking type
    print(type(reader))
    # checking directory
    print(dir(reader))  # __iter__
    print()

    for c in reader:
        # print(c)
        # checking type
        print(type(c))
        # list to str
        print(' : '.join(c))

When csv file is divided by |

예제2
with open('./resource/test2.csv', 'r') as f:
    reader = csv.reader(f, delimiter='|')  # this chooses what will play the role of a comma 
    # next(reader) Header skip
    # check

    for c in reader:
        # print(c)
        print(''.join(c))

transfer to dict form

with open('./resource/test1.csv', 'r') as f:
    reader = csv.DictReader(f)
    # check
    print(reader)
    print(type(reader))
    print(dir(reader))  # __iter__ (iterable)
    print()

    for c in reader:
        for k, v in c.items():
            print(k, v)
        print('-----')

csv file write

w = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18], [19, 20, 21]]

with open('./resource/write1.csv', 'w', encoding='utf-8') as f:
    print(dir(csv))
    wt = csv.writer(f)
    # dir 
    print(dir(wt))
    # type
    print(type(wt))
    for v in w:
        wt.writerow(v) # it's writing v in a row

writing fields

with open('./resource/write2.csv', 'w', newline='') as f:
    # field name
    fields = ['one', 'two', 'three']
    # Dict Writer declaration
    wt = csv.DictWriter(f, fieldnames=fields)
    # Herder Write
    wt.writeheader ()

    for v in w:
        wt.writerow({'one': v[0], 'two': v[1], 'three': v[2]})