파이썬
os module
simstealer
2021. 12. 8. 17:25
os를 제어할 수 있는 방법
1. 환경 변수 값 확인
>>> import os >>> os.environ['PATH'] '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/root/bin' |
2. 디렉토리 위치 변경
>>> os.getcwd() # 현재 경로 확인 '/python >>> os.chdir('/root') # 경로 변경 >>> os.getcwd() '/root' >>> os.listdir('.') #. 경로에 있는 파일, 폴더 리스트 ['192.168.10.0.xml', '.set', '.mozilla', 'version.rc', '.ssh', '192.168.10.0.gnmap', 'rsh-client_0.17-17+b1_amd64.deb', '.wget-hsts', '.zenmap', 'file1.txt', '.python_history', 'scan1.xml', '공개', '.msf4', 'kali-archive-keyring_2018.1_all.deb', 'paros', '비디오', 'debug_results.txt', '.bash_history', '.local', '.armitage.prop', '.lesshst', '192.168.10.0.nmap', '.viminfo', '.gnupg', '.profile', '.john', '사진', '.armitage', '.subversion', '.PyCharmCE2018.1', '.config', 'nmap_output', '.ICEauthority', 'driftnet-0.png', 'ip', '.rnd', '.cache', '문서', 'PycharmProjects', 'bin', '.gconf', '다운로드', '음악', 'hello.py', '.vimrc', 'soldesk.com_ips.txt', 'ip.1', 'venv', '바탕화면', '.java', '서식', '.bashrc', '.psql_history'] >>> os.listdir('/etc') # /etc 밑에 있는 파일, 폴더 리스트 ..... (출력내용 생략) ..... |
3. 명령어 호출
>>> os.system('clear') # 화면을 clear 한다. >>> os.system('ls -l') # OS의 디렉토리 목록을 확인한다. >>> f = os.popen('cat /etc/hosts') # cat /etc/hosts 출력결과를 f 변수에 저장 >>> f.read() # 읽어 들인 결과값을 확인한다. ..... (출력내용 생략) ..... |
4. 파일이 존재하는지 확인
import os import sys if os.path.exists(output_filename): print('This is overwrite the file %s. (C)ontinue or (Q)uit?' % output_filename) response = input('> ') if not response.lower().startswith('c'): # input으로 받은 문자를 lower() 소문자로 변경하고 startswith() 시작문자가 "c"가 not 아니면 종료! sys.exit(2) print("Overwrite...") |