본문 바로가기
카테고리 없음

Operating System Interface

by EasyGPT 2023. 12. 17.
반응형

Operating System Interface

os 모듈은 운영체제와 상호작용하기 위한 수십 가지 함수를 제공:

The os module provides dozens of functions for interacting with the operating system:

>>> import os
>>> os.getcwd() # Return the current working directory 현재 작업 디렉토리
'C:\\Users\\user'
>>> os.chdir('/server/accesslogs') # Change current working directory
>>> os.system('mkdir today') # Run the command mkdir in the system shell
0

from os import * 대신 import os 스타일을 사용해야 합니다.

Be sure to use the import os style instead of from os import *.

이렇게 하면 os.open()이 전혀 다르게 작동하는 내장 open() function가 섀도잉되는 것을 방지할 수 있습니다.

This will keep os.open() from shadowing the built-in open() function which operates much differently.

built-in dir()help() functions는 os와 같은 대형 모듈 작업을 위한 대화식 보조 도구로 유용:

The built-in dir() and help() functions are useful as interactive aids for working with large modules like os:

>>> import os
>>> dir(os)
<returns a list of all module functions>
>>> help(os)
<returns an extensive manual page created from the module's docstrings>

일상적 파일 및 디렉토리 관리 작업을 위해 shutil 모듈은 사용하기 쉬운 더 높은 수준의 인터페이스를 제공:

For daily file and directory management tasks, the shutil module provides a higher level interface that is easier to use:

 
>>> import shutil
>>> shutil.copyfile('data.db', 'archive.db')
'archive.db'
>>> shutil.move('/build/executables', 'installdir')
'installdir'
 
>>> import glob
>>> glob.glob('*.py')
['primes.py', 'random.py', 'quote.py']

Command Line Arguments

일반적 유틸리티 스크립트는 종종 명령줄 인수를 처리해야 합니다.

이러한 인수는 sys 모듈의 argv 속성에 목록으로 저장됩니다.

예를 들어 다음 출력은 명령 줄에서 python demo.py one two three를 실행한 결과:

Common utility scripts often need to process command line arguments. These arguments are stored in the sys module’s argv attribute as a list. For instance the following output results from running python demo.py one two three at the command line:

 
>>> import sys
>>> print(sys.argv)
['demo.py', 'one', 'two', 'three']

argparse 모듈은 명령 줄 인수를 처리하는 보다 정교한 메커니즘을 제공합니다.

The argparse module provides a more sophisticated mechanism to process command line arguments.

다음 스크립트는 하나 이상의 파일 이름과 표시할 선택적 행 수를 추출:

The following script extracts one or more filenames and an optional number of lines to be displayed:

import argparse
parser = argparse.ArgumentParser(prog = 'top',
description = 'Show top lines from each file')
parser.add_argument('filenames', nargs='+')
parser.add_argument('-l', '--lines', type=int, default=10)
args = parser.parse_args()
print(args)

python top.py --lines=5 alpha.txt beta.txt를 명령 줄에서 실행할 때, 스크립트는 args.lines to 5 그리고 args.filenames to ['alpha.txt', 'beta.txt']로 설정합니다.

When run at the command line with python top.py --lines=5 alpha.txt beta.txt, the script sets args.lines to 5 and args.filenames to ['alpha.txt', 'beta.txt'].

https://smartstore.naver.com/dopza/products/4569179898

 

반응형

댓글