티스토리 뷰
현재 폴더의 파일명을 일괄로 변경하는 스트립트는 다음과 같다.
os의 rename(), listdir() 함수를 이용한다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import sys | |
from os import rename, listdir | |
# 현재 위치의 파일 목록 | |
files = listdir('.') | |
# 파일명에 번호 추가하기 | |
count = 0 | |
for name in files: | |
# 파이썬 실행파일명은 변경하지 않음 | |
if sys.argv[0].split("\\")[-1] == name: | |
continue | |
new_name = name.replace(".", "{0:03d}.".format(count)) | |
rename(name,new_name) | |
count += 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
import os | |
def rename_file(): | |
# 현재 위치(.)의 파일을 모두 가져온다. | |
for filename in os.listdir("."): | |
# 파일 확장자가 (properties)인 것만 처리 | |
if filename.endswith("properties"): | |
# 파일명에서 AA를 BB로 변경하고 파일명 수정 | |
new_filename = filename.replace("AA", "BB") | |
os.rename(filename, new_filename) | |
if __name__ == "__main__": | |
rename_file() |
반응형
'python' 카테고리의 다른 글
[python] 셔뱅(Shebang)을 이용하여 파이썬 실행 버전 지정하기 (0) | 2018.08.08 |
---|---|
[python] 반복적인 입력에는 input() 대신 sys.stdin.readline 로 변경 (0) | 2018.06.18 |
[python] [bs4] BeautifulSoup([your markup]) 경고 수정하기 (0) | 2017.12.10 |
[python] multiprocessing 을 이용한 스레드 처리 (0) | 2017.12.07 |
[python] GIL(Global Interpreter Lock) (0) | 2017.11.21 |
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 알고리즘
- 정올
- yarn
- hbase
- Hadoop
- nodejs
- Tez
- 백준
- mysql
- 파이썬
- AWS
- HIVE
- java
- 하둡
- Python
- Linux
- oozie
- SPARK
- 하이브
- 오류
- error
- S3
- ubuntu
- bash
- emr
- 다이나믹
- airflow
- HDFS
- build
- SQL
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함