본문 바로가기
국비교육

Python 5일차

by Diligejy 2019. 5. 7.

1.

# -*- coding:utf-8 -*-
import os
res = os.access('.', os.W_OK | os.X_OK | os.R_OK)
print(res)
print(os.getcwd())  # 현재 디렉토리를 리턴
os.chdir('c:\Test\MyPython')  # 디렉토리 위치를 변경
print(os.getcwd())  # 변경된 현재 디렉토리를 리턴

2.

3.

# 1. c:\mytest 만들기

from os.path import *

try:

    rpath = "c:/mytest"

    if exists(rpath):

        print('이미 만들었어')

    else:

        os.mkdir(rpath, os.W_OK|os.R_OK, dir_fd = None)

except OSError as e:

    print("Exception : OSError", e)

 

#2. c:/mytest/a.txt

 

open('c:/mytest/b.txt', 'w+')

 

#3 목록을 확인

for path, dirs, files in os.walk('C:\mytest'):

    print(path, dirs, files)

 

4.

#-*- codiing:utf=8 -*-

import os, time
from os.path import exists

os.path.join('c:m','a.txt')
os.path.join(r'c:\\m','a.txt')

print(os.listdir('c:/'))

path='c:/Test'
file_list=os.listdir(path)
for file in file_list:
    file = os.path.join(path,file)
    print(file,os.stat(file).st_size,time.ctime(os.stat(file).st_mtime))

 

5.

#-*- codiing:utf=8 -*-

import os, time
from os.path import exists

os.path.join('c:m','a.txt')
os.path.join(r'c:\\m','a.txt')

print(os.listdir('c:/'))

path='c:/Test'
file_list=os.listdir(path)
for file in file_list:
    file = os.path.join(path,file)
    print(file,os.stat(file).st_size,time.ctime(os.stat(file).st_mtime))

 

6.

import os.path

file = 'c:/Test/location.txt'

if __name__ == '__main__':
    if os.path.isfile(file):
        a = os.path.basename(file)
        b = os.path.split(file)
        c = os.path.normpath(file)
        print(a)
        print(b)
        print(c)

 

7.

# -*- coding:utf-8 -*-
import os, time

file = 'c:/Test/MyPython/input.txt'
print(os.path.getctime(file))  # 파일 생성시간
print(time.ctime(os.path.getctime(file)))
print()
print(time.ctime(os.path.getatime(file)))  # 엑세스 시간
print(time.ctime(os.path.getmtime(file)))  # 변경 시간
print(os.path.getsize(file))  # 파일의 사이즈
print(os.path.splitext(file))  # 파일명과 확장자 분리

 

8.

readline - string

readlines - list

 

 

9.

순수하게 binary 코드를 쓰고 싶다면 write()메소드를 써줘야 한다.

그렇지 않고 writelines()를 쓰면 object를 요구하기 때문에 write()메소드를 써줄 필요가 있다.

 

10.

 

'국비교육' 카테고리의 다른 글

Python 7일차  (0) 2019.05.09
Python 6일차  (0) 2019.05.08
Python 4일차  (0) 2019.05.03
Python 3일차  (0) 2019.05.02
Python 2일차  (0) 2019.04.30

댓글