파이썬/파일
python(vscode)/폴더명 바꾸기/os.rename
gongdol
2023. 1. 7. 22:04
300x250
폴더리스트를 가져온 후 폴더이름을 바꿔보자.
1. 참고 : 폴더 리스트 가져오기
https://gongdolgongdol.tistory.com/134
python(vscode)/폴더명 리스트 가져오기/원하는 경로 지정
특정 경로에 있는 폴더면 리스트를 가져와보자. 1. 폴더 경로 정하기 1) 아래 이미지내의 경로의 파일,폴더 이름들을 가져와보자. 2. 코드작성 1 2 3 4 5 6 import os filepath = r"C:\Users\Documents\python_test\sto
gongdolgongdol.tistory.com
2. 기존폴더명 확인 - 코드작성전 이름 바꿀 폴더를 정하자
1) 아래 디렉토리의 1,2 폴더명을 변경해보자.
3. 코드작성
1) 기존폴더리스트에 오늘 날짜 22/1/7 을 붙이는 것으로 폴더 이름을 변경해보자.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
import os
filepath = r"C:\Users\Documents\python_test\stock_study"
file_list = os.listdir(filepath)
forder_list =[]
for item in file_list:
sub_folder = os.path.join(filepath, item) # path 합치기
if os.path.isdir(sub_folder): # 폴더 여부 확인
forder_list.append(item)
change_name = os.path.join(filepath, "230107"+item)
os.rename(sub_folder, change_name)
|
cs |
4. 결과
1) 기존 폴더이름 에 오늘날짜를 붙였다. 완료
300x250