강좌
클라우드/리눅스에 관한 강좌입니다.
프로그램 분류

파이썬기초83 : 파일입출력03

작성자 정보

  • 관리자 작성
  • 작성일

컨텐츠 정보

본문

from os.path import *

import time, glob


os.chdir("C:\work")

os.getcwd()

Out[]: 'C:\\work'

os.listdir()

Out[]: 

['.vscode',

 'chromedriver.exe',

 'commit2.db',

 'data.txt',

 'demo.docx',

 'example.xls',

 'example2.xls',

 'example3.xls',

 'hello.html',

 'PV3.txt',

 'PV3_copy.txt',

 'test.db']


os.listdir(os.getcwd())

Out[]: 

['AppData',

 'Application Data',

 'article.docx',

 'articles.docx',

 'Contacts',

 'Cookies',

 'demo2.docx',

 'Desktop',

 'Documents',

 'Downloads']


os.getcwd()

Out[]: 'C:\\Users\\User'

os.chdir(os.getcwd())

os.getcwd()

Out[]: 'C:\\Users\\User'


os.path.isdir(os.getcwd() + '/isdecimal.py')

Out[]: False


os.path.join('usr', 'bin', 'local', 'tmp')

Out[]: 'usr\\bin\\local\\tmp'


os.path.join(os.getcwd(), 'isdecimal.py')

Out[]: 'C:\\Users\\User\\isdecimal.py'



#현재경로 확인하기

os.getcwd()       

Out[]: 'C:\\work'


#현재경로에 경로 더하기

abspath('work')                 

Out[]: 'C:\\work\\work'


# 경로 변경하여 변경된 경로 확인하기

os.chdir('c:\\')  

os.getcwd()      

Out[]: 'c:\\'


#경로변경하여 변경된 현재경로 확인

os.chdir('c:\\work')            

os.getcwd()                       

Out[]: 'c:\\work'


#현재경로에 경로더하기

abspath('tmp')                      

Out[]: 'c:\\work\\tmp'


# 현재디렉토리명 또는 파일명만 구하기

basename('C:/Python35/tmp')     

Out[]: 'tmp'


basename('C:/Python35/PycharmProjects/FileSystem_source/file_IO.py')

Out[]: 'file_IO.py'


# 현재위치에서의 디렉토리경로까지만 구하기

dirname('C:/Python35/PycharmProjects/FileSystem_source/file_IO.py')

Out[]: 'C:/Python35/PycharmProjects/FileSystem_source'


# 지정한 디렉토리나파일이 존재하는가?

exists('C:\\work\\imsitxt.txt')

Out[]: False


exists('c:\\work')

Out[]: True


# 파일정보(시간)

getctime('test.txt')

Out[]: 1543147467.3569753


# 파일정보(사이즈)

getsize('test.txt')

Out[]: 15


# 절대경로인가?

isabs('C:\\work\\test.txt')

Out[]: True


isabs('test.txt')

Out[]: False


# 지정한경로가 파일인가?

isfile('c:\\work\\test.txt')

Out[]: True


isfile('c:\\work')

Out[]: False



# 지정한경로가 디렉토리인가?

isdir('c:\\work')

Out[]: True


# 경로만들기

join('c:\\work','subdir','my.txt')

Out[]: 'c:\\work\\subdir\\my.txt'


# 경로 소문자로 만들기

normcase('c:\\work')

Out[]: 'c:\\work'


normcase('c:\\Work')

Out[]: 'c:\\work'


normcase('c:\\WORK')

Out[]: 'c:\\work'


#경로 소문자경로만들기

normpath('c:\\work')

Out[]: 'c:\\work'


# 경로 각각 분리하기

split('c:\\work\\test.txt')

Out[]: ('c:\\work', 'test.txt')


splitext('c:\\work\\test.txt')

Out[]: ('c:\\work\\test', '.txt')


# 리스팅하여 요소들을 리스트에 각각 저장하기

glob.glob('*.*')

Out[]: 

['chromedriver.exe',

 'commit2.db',

 'data.txt',

 'demo.docx',

 'example.xls',

 'example2.xls',

 'example3.xls',

 'hello.html',

 'mytext.txt',

 'new.txt',

 'PV3.txt',

 'PV3_copy.txt',

 'test.db',

 'test.txt']


glob.glob('*.txt')

Out[]: ['data.txt', 'mytext.txt', 'new.txt', 'PV3.txt', 'PV3_copy.txt', 'test.txt']


glob.glob('*.exe')

Out[]: ['chromedriver.exe']


# 지정한 경로를 리스팅하여 디렉토리만들 출력하기

for x in glob.glob('C:/*'):

    if isdir(x):

        print(x)

        

C:/$Recycle.Bin

C:/Config.Msi

C:/Documents and Settings

C:/HanaTaxDownload

C:/Intel

C:/MSOCache

C:/mymodule

C:/OpenSSL

C:/OpenSSL-Win64

C:/PerfLogs


# 지정한 경로를 리스팅하여 파일들만 출력하기

for x in glob.glob('C:/*'):

    if isfile(x):

        print(x)

        

C:/bootmgr

C:/BOOTNXT

C:/hiberfil.sys

C:/pagefile.sys

C:/swapfile.sys



# 리스팅을 하나씩 출력할 수 있는 generator 객체 생성

glob.iglob('*')

Out[]: <generator object _iglob at 0x000001D62C229200>


for i in glob.iglob('*'):

    print(i)

    

chromedriver.exe

commit2.db

data.txt

demo.docx

example.xls

example2.xls


관련자료

댓글 0
등록된 댓글이 없습니다.

공지사항


뉴스광장


  • 현재 회원수 :  60,037 명
  • 현재 강좌수 :  35,810 개
  • 현재 접속자 :  119 명