OS 모듈 Python에서는 운영 체제와 상호 작용하는 기능을 제공합니다. OS는 Python의 표준 유틸리티 모듈에 속합니다. 이 모듈은 운영 체제 종속 기능을 사용하는 이식 가능한 방법을 제공합니다.
Python에서 파일이나 디렉터리의 이름을 바꾸려면 다음을 사용할 수 있습니다. os.이름 바꾸기() OS 모듈의 기능. 이 방법은 소스 파일이나 디렉터리의 이름을 지정된 대상 파일이나 디렉터리로 바꿉니다. 두 가지 매개변수가 필요합니다 – 원천 (현재 파일 이름) 및 목적지 (새 파일 이름).
통사론 :
os.rename(원본, 대상, *, src_dir_fd = 없음, dst_dir_fd = 없음)
자바 문자열 비교
매개변수:
- 원천: 파일 시스템 경로를 나타내는 경로류 객체입니다. 이름을 바꿀 소스 파일 경로입니다.
- 목적지: 파일 시스템 경로를 나타내는 경로류 객체입니다.
- src_dir_fd(선택 사항): 디렉터리를 참조하는 파일 설명자입니다.
- dst_dir_fd(선택 사항): 디렉터리를 참조하는 파일 설명자입니다.
반환 유형:
이 메서드는 어떤 값도 반환하지 않습니다.
os.rename() 함수 및 오류 처리 사용:
os.rename 함수를 사용하는 방법에 대한 프로그램을 살펴보겠습니다. OS 모듈 그리고 그것을 사용하는 동안 오류를 처리하는 방법.
코드 1: 사용 os.이름 바꾸기() 방법.
파이썬3
int를 더블 자바로 변환
# Python program to explain os.rename() method> # importing os module> import> os> # Source file path> source> => 'techcodeview.com/file.txt'> # destination file path> dest> => 'GeekforGeeks/newfile.txt'> # Now rename the source path> # to destination path> # using os.rename() method> os.rename(source, dest)> print> ('Source path renamed to destination path successfully.')> |
>
>
코드 2: 발생 가능한 오류 처리
파이썬3
# Python program to explain os.rename() method> # importing os module> import> os> # Source file path> source> => './techcodeview.com/file.txt'> # destination file path> dest> => './techcodeview.com/dir'> # try renaming the source path> # to destination path> # using os.rename() method> try> :> > os.rename(source, dest)> > print> ('Source path renamed to destination path successfully.')> # If Source is a file> # but destination is a directory> except> IsADirectoryError:> > print> ('Source> is> a> file> but destination> is> a directory.')> # If source is a directory> # but destination is a file> except> NotADirectoryError:> > print> ('Source> is> a directory but destination> is> a> file> .')> # For permission related errors> except> PermissionError:> > print> ('Operation> not> permitted.')> # For other errors> except> OSError as error:> > print> (error)> |
>
>
다중 테이블 SQL 선택
이 기사에서는 os.rename() 함수를 사용하여 Python에서 파일이나 디렉터리의 이름을 바꾸는 방법을 다뤘습니다. 이는 Python에서 파일이나 디렉터리의 이름을 바꾸는 매우 쉽고 간단한 방법입니다. OS 모듈은 운영 체제와 상호 작용하는 데 사용되는 기능 목록을 제공합니다.