Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 |
Tags
- IOS
- python
- flutter
- ubuntu
- swift
- 리눅스
- port
- ASP
- PER
- 라즈베리파이
- MySQL
- 날짜
- Linux
- node.js
- javascript
- Unity
- 다이어트
- mssql
- urllib
- PyQt5
- 유니티
- 맛집
- Excel
- MS-SQL
- pandas
- sqlite
- GIT
- PyQt
- tensorflow
- 함수
Archives
아미(아름다운미소)
[python] 파일, 디렉터리 조작 본문
[python] 파일, 디렉터리 조작
디렉터리, 파일, 확장자 분리 (get directory and file, extension)import os
fullpath = '/home/aaa/bbb/ccc.txt'
print(os.path.dirname(fullpath)) # '/home/aaa/bbb'
print(os.path.basename(fullpath)) # 'ccc.txt'
print(os.path.split(fullpath)) # ('/home/aaa/bbb', 'ccc.txt')
print(os.path.splitext(fullpath)) # ('/home/aaa/bbb/ccc', '.txt')
print(os.path.exists('/home/') # True
print(os.path.exists('/home/not-exists-file') # False
import shutil
shutil.copy('a.txt', 'a-copied.txt')
import shutil
shutil.move('a.txt', 'b.txt')
import os
os.remove('a.txt')
import shutil
shutil.copytree('src_dir', 'tar_dir')
import shutil
shutil.move('a_dir', 'b_dir')
import shutil
shutil.rmtree('a_dir')
import os
cwd = os.getcwd()
print('current working dir: %s' % cwd)
for filename in os.listdir(cwd):
fullpath = os.path.join(cwd, filename)
if (os.path.islink(fullpath)):
print('link: %s' % fullpath)
elif (os.path.isfile(fullpath)):
print('file: %s' % fullpath)
elif (os.path.isdir(fullpath)):
print('dir: %s' % fullpath)
import os
cwd = os.getcwd()
for (path, dirs, files) in os.walk(cwd):
for f in files:
fullpath = os.path.join(path, f)
print(fullpath)
os.walk(dirpath)는 전달된 경로뿐만 아니라 해당 경로의 하위에 있는 모든 디렉터리 정보까지 제공해 줍니다.
'랭귀지 > python' 카테고리의 다른 글
| 파이썬에서 cv2 파일의 프레임 정보를 확인하는 법 (0) | 2018.01.20 |
|---|---|
| 파이썬의 OpenPyXL을 이용한 엑셀 문서 처리 (3) | 2018.01.15 |
| [python] PyMySQL을 이용한 MySQL (0) | 2018.01.10 |
| python으로 REST API 호출 (0) | 2018.01.05 |
| 파이썬 이미지 처리(Pillow) (0) | 2018.01.03 |
Comments