본문 바로가기
IT/프로그래밍

파이썬 엑셀 파일 읽고 쓰기 - 내 일을 바꾸는 업무 자동화 5장

by nutrient 2021. 5. 29.
728x90
728x170

 

 

5.4-1

from os import listdir

files = listdir("C:\\example")
print(files)

 

5.4-2

from os.path import exists

if exists("C:\\Python27"):
    print("Python 2.7 is installed")


 

5.4-3

from os import makedirs

makedirs("C:\\example")

 

5.4-4

from shutil import copyfile

copyfile("C:\\Downloads\\download.txt", "C:\\Downloads\\copied.txt")

 

5.4-5

from shutil import copytree

copytree("C:\\Downloads ", "C:\\Copied_Downloads")

 

5.4-6

from shutil import rmtree

rmtree("C:\\Copied_Downloads")

 

5.4-7

from os import unlink

unlink("C:\\Downloads\\download.txt")

 

5.6

from os import listdir

write_file = open('statistics.csv', 'w')
write_file.write('년월,매출\n')

csv_files = 'csv_files/'
file_list = listdir(csv_files)
file_list.sort()

for f_name in file_list:
    if f_name[-3:] != 'csv':
        continue

    sum_value = 0
    f = open(csv_files+f_name, 'r')
    while True:
        row = f.readline()
        if not row:
            break

        data = row.split(',')
        if data[1].isdigit():
            sum_value += int(data[1])

    write_file.write('%s,%d\n'%(f_name[:7], sum_value))
    f.close()

write_file.close()

 

5.7-1

f = open('test.txt', 'w')
f.write('문자열 쓰기\n')
f.close()

 

5.7-2

f = open('test.txt', 'a')
f.write('문자열 추가하기\n')
f.close()

 

5.7-3

f = open('test.txt', 'r')
contents = f.readline()
print(contents)
f.close()

 

5.7-4

f = open('test.txt', 'r')
contents = f.readlines()
print(contents)
f.close()

 

5.7-5

f = open('test.txt', 'r')
contents = f.read()
print(contents)
f.close()

 

5.7-6

f = open('test.txt', 'r+')
contents = f.read()
print(contents)
f.write('문자열 테스트\n')
contents = f.read()
print(contents)
f.close()

 

5.7-6-2

f = open('test.txt', 'r+')
contents = f.read()
print(contents)
f.write('새롭게 추가한 문자열\n')
contents = f.read()
print(contents)
f.close()

 

5.7-7

f = open('test.txt', 'r+')
contents = f.read()
print(contents)
f.write('문자열 추가 테스트\n')
f.seek(0)
contents = f.read()
print(contents)
f.close()

 

auto_file.py

from shutil import copyfile
from os import listdir, makedirs
from os.path import isdir

orig_dir = "C:\\scandata\\"
out_dir  = "C:\\organized\\"

file_list = listdir(orig_dir)

for f_name in file_list:
    f_date = f_name[5:-4]
    f_date = f_date.split('_')
    f_date = f_date[0]
    f_date = f_date.split('-')

    year  = f_date[0]
    month = f_date[1]
    day   = f_date[2]

    target_dir = out_dir + year + "\\" + month + "\\" + day
    if not isdir(target_dir):
        makedirs(target_dir)

    copyfile(orig_dir+f_name, target_dir+"\\" + f_name)
    print(orig_dir+f_name + " => " + target_dir+"\\" + f_name)

 

2017.01.csv
0.00MB
2017.02.csv
0.00MB
2017.03.csv
0.00MB
2017.04.csv
0.00MB
2017.05.csv
0.00MB
2017.06.csv
0.00MB
2017.07.csv
0.00MB
2017.08.csv
0.00MB
2017.09.csv
0.00MB
2017.10.csv
0.00MB
2017.11.csv
0.00MB
2017.12.csv
0.00MB
statistics.csv
0.00MB
test.txt
0.00MB

728x90
그리드형

댓글