logo

Python OS 모듈

Python OS 모듈은 사용자와 운영 체제 간의 상호 작용을 설정하는 기능을 제공합니다. OS 기반 작업을 수행하고 운영 체제에 대한 관련 정보를 얻는 데 사용되는 많은 유용한 OS 기능을 제공합니다.

OS는 Python의 표준 유틸리티 모듈에 속합니다. 이 모듈은 운영 체제 종속 기능을 사용하는 이식 가능한 방법을 제공합니다.

Python OS 모듈을 사용하면 파일 및 디렉터리 작업을 수행할 수 있습니다.

 To work with the OS module, we need to import the OS module. import os 

OS 모듈에는 다음과 같은 몇 가지 기능이 있습니다.

OS.이름()

이 함수는 가져오는 운영 체제 모듈의 이름을 제공합니다.

현재 'posix', 'nt', 'os2', 'ce', 'java', 'riscos'가 등록되어 있습니다.

 import os print(os.name) 

산출:

 nt 

os.mkdir()

그만큼 os.mkdir() 함수는 새 디렉터리를 만드는 데 사용됩니다. 다음 예를 고려하십시오.

 import os os.mkdir('d:\newdir') 

폴더 newdir이라는 D 드라이브에 있는 함수의 문자열 인수에 있는 경로에 새 디렉터리를 생성합니다.

os.getcwd()

파일의 현재 작업 디렉터리(CWD)를 반환합니다.

 import os print(os.getcwd()) 

산출:

 C:UsersPythonDesktopModuleOS 

os.chdir()

그만큼 모듈은 다음을 제공합니다. chdir() 현재 작업 디렉토리를 변경하는 함수입니다.

JSON 파일
 import os os.chdir('d:\') 

산출:

 d:\ 

os.rmdir()

그만큼 rmdir() 함수는 절대 또는 관련 경로를 사용하여 지정된 디렉터리를 제거합니다. 먼저 현재 작업 디렉터리를 변경하고 폴더를 제거해야 합니다.

 import os # It will throw a Permission error; that's why we have to change the current working directory. os.rmdir('d:\newdir') os.chdir('..') os.rmdir('newdir') 

OS.오류()

os.error() 함수는 OS 수준 오류를 정의합니다. 유효하지 않거나 액세스할 수 없는 파일 이름 및 경로 등의 경우 OSError가 발생합니다.

 import os try: # If file does not exist, # then it throw an IOError filename = 'Python.txt' f = open(filename, 'rU') text = f.read() f.close() # The Control jumps directly to here if # any lines throws IOError. except IOError: # print(os.error) will print('Problem reading: ' + filename) 

산출:

 Problem reading: Python.txt 

os.popen()

이 함수는 파일을 열거나 지정된 명령을 통해 파이프에 연결된 파일 객체를 반환합니다.

 import os fd = 'python.txt' # popen() is similar to open() file = open(fd, 'w') file.write('This is awesome') file.close() file = open(fd, 'r') text = file.read() print(text) # popen() provides gateway and accesses the file directly file = os.popen(fd, 'w') file.write('This is awesome') # File not closed, shown in next function. 

산출:

 This is awesome 

os.close()

이 함수는 설명자와 관련된 파일을 닫습니다. 정말로 .

 import os fr = 'Python1.txt' file = open(fr, 'r') text = file.read() print(text) os.close(file) 

산출:

 Traceback (most recent call last): File 'main.py', line 3, in file = open(fr, 'r') FileNotFoundError: [Errno 2] No such file or directory: 'Python1.txt' 

os.이름 바꾸기()

함수를 사용하여 파일이나 디렉터리의 이름을 바꿀 수 있습니다. os.이름 바꾸기() . 사용자는 파일을 변경할 수 있는 권한이 있는 경우 파일 이름을 바꿀 수 있습니다.

 import os fd = 'python.txt' os.rename(fd,'Python1.txt') os.rename(fd,'Python1.txt') 

산출:

 Traceback (most recent call last): File 'main.py', line 3, in os.rename(fd,'Python1.txt') FileNotFoundError: [Errno 2] No such file or directory: 'python.txt' -> 'Python1.txt' 

os.액세스()

이 함수는 실수를 사용합니다. uid/gid 호출하는 사용자가 경로에 액세스할 수 있는지 테스트합니다.

 import os import sys path1 = os.access('Python.txt', os.F_OK) print('Exist path:', path1) # Checking access with os.R_OK path2 = os.access('Python.txt', os.R_OK) print('It access to read the file:', path2) # Checking access with os.W_OK path3 = os.access('Python.txt', os.W_OK) print('It access to write the file:', path3) # Checking access with os.X_OK path4 = os.access('Python.txt', os.X_OK) print('Check if path can be executed:', path4) 

산출:

 Exist path: False It access to read the file: False It access to write the file: False Check if path can be executed: False