ãããããã°ããã¯ãPythonã®æ¨æºã¢ã¸ã¥ã¼ã«ã®ãã¡ãè¯ã使ããããã®ã«ã¤ãã¦ããã®ä½¿ãæ¹ãè¦ã¦ããã¾ãã
ã©ã®è¨èªã§ãããã§ãããè¨èªãã®ãã®ã ãã§ã¯ä¸ååã§ãè¨èªã®æä¾ããã¢ã¸ã¥ã¼ã«ããããã¯ã©ã¤ãã©ãªãå¿
è¦ä¸å¯æ¬ ã§ãã
ä»åã®è¨äºã§ã¯ãPythonã®os
ã¢ã¸ã¥ã¼ã«ã®åºæ¬ã¨æ´»ç¨æ¹æ³ãå¦ã³ã¾ãã
Pythonã®os
ã¢ã¸ã¥ã¼ã«ã¯ããªãã¬ã¼ãã£ã³ã°ã·ã¹ãã ã¨ã®ããåããå¯è½ã«ããæ¨æºã¢ã¸ã¥ã¼ã«ã§ãã
Pythonã¯ãWindowsãMacOSãLinuxãFreeBSDãªã©æ§ã
ãªOSã®ä¸ã§åãã¦ãã¾ãã
Pythonã¯ããããã®OSéã«éããã«ãã¼ããã§ããã ãæ¨æºçãªæç¶ãã§ã¢ã¯ã»ã¹ããããã®ã¢ã¸ã¥ã¼ã«ã¨ãã¦os
ãç¨æãã¦ãã¾ãã
ãã®ã¢ã¸ã¥ã¼ã«ã¯ãOSã®æä¾ãããµã¼ãã¹ãããªãã¡ããã¡ã¤ã«ããã£ã¬ã¯ããªæä½ãç°å¢å¤æ°ã®åå¾ã»è¨å®ãããã»ã¹ç®¡çãªã©ãå¤ãã®ã·ã¹ãã é¢é£ã¿ã¹ã¯ããµãã¼ããã¾ãã
osã¢ã¸ã¥ã¼ã«ã®æ¦è¦
os
ã¢ã¸ã¥ã¼ã«ã¯ãPythonããã°ã©ã ãå®è¡ç°å¢ã®OSï¼Windows, macOS, Linuxãªã©ï¼ã¨å¯¾è©±ããããã®ä¾¿å©ãªã¤ã³ã¿ã¼ãã§ã¼ã¹ãæä¾ãã¾ãã
ä¾ãã°ã以ä¸ã®ãããªã¿ã¹ã¯ãç°¡åã«å®ç¾ã§ãã¾ãã
- ãã¡ã¤ã«ããã£ã¬ã¯ããªã®æä½ï¼ä½æãåé¤ã移åï¼
- ç°å¢å¤æ°ã®åå¾ã»è¨å®
- ããã»ã¹é¢é£ã®æä½
- ã·ã¹ãã æ å ±ã®åå¾
ã¢ã¸ã¥ã¼ã«ãå©ç¨ããã«ã¯ãPythonããã°ã©ã ã§ä»¥ä¸ã®ããã«ã¤ã³ãã¼ããã¾ãã
import os
osã¢ã¸ã¥ã¼ã«ã®ä¸»è¦ãªé¢æ°ãã¡ã½ãããå®æ°
ããã§ã¯ããã使ãããos
ã¢ã¸ã¥ã¼ã«ã®é¢æ°ã¨å®æ°ã«ã¤ãã¦èª¬æãã¾ãã
ããã°ã©ã ä¾ã§ã¯ããã§ã«os
ã¢ã¸ã¥ã¼ã«ãã¤ã³ãã¼ãããã¦ãããã®ã¨ãã¾ãã
ãã¡ã¤ã«ã¨ãã£ã¬ã¯ããªã®æä½
os.getcwd()
ç¾å¨ã®ä½æ¥ãã£ã¬ã¯ããªãåå¾ãã¾ãã
>>> os.getcwd()
'/home/user/project'
os.chdir(path)
ä½æ¥ãã£ã¬ã¯ããªãå¤æ´ãã¾ãã
>>> os.chdir('/tmp') >>> os.getcwd() '/tmp'
os.listdir(path)
æå®ããããã£ã¬ã¯ããªå ã®ãã¡ã¤ã«ããã©ã«ãã®ä¸è¦§ãåå¾ãã¾ãã
>>> os.listdir('.') ['file1.txt', 'dir1', 'script.py']
os.mkdir(path)
æ°ãããã£ã¬ã¯ããªãä½æãã¾ãã
>>> os.mkdir('new_dir') >>> os.listdir('.') ['file1.txt', 'dir1', 'script.py', 'new_dir']
os.remove(path)
ãã¡ã¤ã«ãåé¤ãã¾ãã
>>> os.remove('file1.txt')
os.rmdir(path)
空ã®ãã£ã¬ã¯ããªãåé¤ãã¾ãã
>>> os.rmdir('new_dir')
os.path
ã¢ã¸ã¥ã¼ã«ã¨ã®é£æº
os
ã¢ã¸ã¥ã¼ã«ã®ä¸é¨ã§ããos.path
ã«ã¯ããã¹æä½ã«ä¾¿å©ãªé¢æ°ãå¤ãå«ã¾ãã¦ãã¾ãã
>>> os.path.exists('file1.txt') # ãã¡ã¤ã«ãåå¨ããããã§ã㯠True >>> os.path.isabs('/tmp/file1.txt') # 絶対ãã¹ãã©ãã True >>> os.path.join('/tmp', 'file1.txt') # ãã¹ãçµå '/tmp/file1.txt'
ç°å¢å¤æ°ã®æä½
os.getenv(key, default=None)
ç°å¢å¤æ°ã®å¤ãåå¾ãã¾ãã
>>> os.getenv('HOME') '/home/user'
os.environ
ç°å¢å¤æ°ã®ä¸è¦§ãæã¤ãããåãªãã¸ã§ã¯ãã§ãã è¾æ¸åã®ããã«ã¢ã¯ã»ã¹ãããã¨ãã§ãã¾ãã
>>> os.environ["HOMEDRIVE"] 'C:' >>>
ããã¯ãWIndowsã®ä¾ã§ããLinuxã§ã¯ãã®ãããªçµæã«ã¯ãªããªãã¨æãã¾ãã
ã¾ããç°å¢å¤æ°ã追å ãããã¨ãã§ãã¾ãã
>>> os.environ['MY_VAR'] = 'value' >>> os.getenv('MY_VAR') 'value'
os.putenv(key, value)
ã§ãç°å¢å¤æ°ã追å ãããã¨ãã§ãã¾ãã
å®ã¯ä¸è¨ã®os.environ
ã«ããç°å¢å¤æ°ã®è¿½å æã«ã¯ãos.putenv
ã使ããã¦ããã®ã§ãã
ãããç´æ¥os.putenv
ã使ã£ã¦ç°å¢å¤æ°ã追å ããã¨ãos.environ
ã«ããã¯åæ ããã¾ããã
ã§ããããos.environ
ã®ç´æ¥æä½ã®æ¹ãæ¨å¥¨ããã¦ãã¾ãã
å®æ°
次ã®2ã¤ã¯å®æ°ã§ãã å¤ãã¦ã¼ã¶ãå¤æ´ãããã¨ã¯ã§ãã¾ããã
os.name
ç¾å¨ã®OSã示ãæååãä¾ãã°ã'posix'ï¼Linux, macOSï¼ã'nt'ï¼Windowsï¼ãos.sep
ãã¹ã®åºåãæåãä¾ãã°ã'/'ï¼Unixç³»ï¼ã'\'ï¼Windowsï¼ã
>>> os.name 'posix' >>> os.sep '/'
ããã»ã¹ã®æä½
Pythonã®os
ã¢ã¸ã¥ã¼ã«ã§ã¯ã以ä¸ã®ããã«ããã»ã¹ã«é¢é£ããæä½ãè¡ããã¨ãã§ãã¾ãã
ãããã¯ä½ã¬ãã«ï¼OSã«è¿ãã¬ãã«ï¼ã®ã¡ã½ããã§ããã使ãæ©ä¼ã¯ãã¾ãããã¾ããã
ã§ããããåç´è
ã«ã¨ã£ã¦ã¯ãå¿
è¦ãªæã«ãã§ãã¯ããç¨åº¦ã§è¯ãã§ãããã
os.getpid()
ç¾å¨ã®ããã»ã¹IDãåå¾ãã¾ãã
>>> os.getpid() 12345 # å®è¡ä¸ã®ããã»ã¹ID
os.getppid()
ç¾å¨ã®ããã»ã¹ãçæãã親ããã»ã¹ã®IDãåå¾ãã¾ãã
>>> os.getppid() 6789 # 親ããã»ã¹ã®ID
os.fork()
UNIXç³»OSã§æ°ããããã»ã¹ãçæãã¾ãï¼Windowsã§ã¯ä½¿ç¨ä¸å¯ï¼ã
ãã®é¢æ°ã使ãã¨ãç¾å¨ã®ããã»ã¹ãè¤è£½ããã親ããã»ã¹ã¨åããã»ã¹ã®2ã¤ã®ããã»ã¹ã並è¡ãã¦å®è¡ããã¾ãã
pid = os.fork() if pid == 0: # åããã»ã¹ print("åããã»ã¹") else: # 親ããã»ã¹ print(f"親ããã»ã¹ (åããã»ã¹ã®ID: {pid})")
os.execl()
, os.execv()
, os.execvp()
ç¾å¨ã®ããã»ã¹ãç½®ãæããå½¢ã§ãæå®ããæ°ããããã°ã©ã ãå®è¡ãã¾ãã
ä¾ãã°ã以ä¸ã¯æ°ããããã°ã©ã ã«ç½®ãæããä¾ã§ãã
os.execl('/bin/ls', 'ls', '-l') # lsã³ãã³ããå®è¡
os.kill(pid, signal)
æå®ããããã»ã¹IDã«ã·ã°ãã«ãéãã¾ããããã»ã¹ãçµäºãããããç¹å®ã®ã·ã°ãã«ãå¦çãããã®ã«ä½¿ãã¾ãã
import signal os.kill(12345, signal.SIGTERM) # ããã»ã¹12345ãçµäº
ã·ã¹ãã æ å ±ã®åå¾
os
ã¢ã¸ã¥ã¼ã«ã¯ãã·ã¹ãã ã«é¢ããæ
å ±ãåå¾ããããã®é¢æ°ãæä¾ãã¦ãã¾ãããããã®æ©è½ãæ´»ç¨ãããã¨ã§ãã¹ã¯ãªããã®å®è¡ç°å¢ãåçã«ç¢ºèªããããããã«å¿ããåä½ãå®ç¾ã§ãã¾ãã
os.name
ç¾å¨ã®OSã表ãæååãåå¾ãã¾ãã
>>> os.name 'posix' # ä¾: Linuxç°å¢ã§ã®åºå
os.uname()
ã·ã¹ãã æ
å ±ã表ããªãã¸ã§ã¯ããåå¾ãã¾ãï¼Windowsã§ã¯ä½¿ç¨ä¸å¯ï¼ã
以ä¸ã®å±æ§ãæã¤ãªãã¸ã§ã¯ããè¿ããã¾ãã
sysname
: OSã®åånodename
: ãããã¯ã¼ã¯årelease
: OSãªãªã¼ã¹çªå·version
: OSãã¼ã¸ã§ã³machine
: ãã¼ãã¦ã§ã¢å
>>> os.uname() posix.uname_result(sysname='Linux', nodename='hostname', release='5.4.0', version='#42-Ubuntu SMP', machine='x86_64')
os.getenv()
ç°å¢å¤æ°ã®å¤ãåå¾ãã¾ããHOME
ãPATH
ãªã©ã®ç°å¢å¤æ°ã®å¤ã調ã¹ãã®ã«ä¾¿å©ã§ãã
>>> os.getenv('HOME') # ã¦ã¼ã¶ã¼ã®ãã¼ã ãã£ã¬ã¯ã㪠'/home/user'
osã¢ã¸ã¥ã¼ã«ã®å ¸åçãªä½¿ãæ¹
ä¸æãã£ã¬ã¯ããªã®æä½
ä¸æçãªãã¡ã¤ã«ãæ±ãå ´åãtempfile
ã¢ã¸ã¥ã¼ã«ã¨çµã¿åããããã¨ãä¸è¬çã§ãããos
ã¢ã¸ã¥ã¼ã«ã ãã§ãååã«å®ç¾å¯è½ã§ãã
import os temp_dir = 'my_temp_dir' # ä¸æãã£ã¬ã¯ããªãä½æ if not os.path.exists(temp_dir): os.mkdir(temp_dir) # ä½æ¥ãã£ã¬ã¯ããªã移å os.chdir(temp_dir) print(f"ç¾å¨ã®ä½æ¥ãã£ã¬ã¯ããª: {os.getcwd()}")
osã¢ã¸ã¥ã¼ã«ã使ã£ãå®ç¨çãªä¾
ãã¡ã¤ã«ã®ããã¯ã¢ããã¹ã¯ãªãã
次ã®ã¹ã¯ãªããã¯ãæå®ããããã£ã¬ã¯ããªå ã®ãã¹ã¦ã®ãã¡ã¤ã«ãããã¯ã¢ããç¨ãã£ã¬ã¯ããªã«ã³ãã¼ãã¾ãã ãã ããä¸è¬ãã¡ã¤ã«ä»¥å¤ï¼ä¾ãã°ãã£ã¬ã¯ããªï¼ã¯ã³ãã¼ãã¾ããã
import os import shutil def backup_files(src_dir, backup_dir): # ããã¯ã¢ãããã£ã¬ã¯ããªãåå¨ããªããã°ä½æ if not os.path.exists(backup_dir): os.mkdir(backup_dir) # ãã¡ã¤ã«ãããã¯ã¢ãã for f in os.listdir(src_dir): src_path = os.path.join(src_dir, f) backup_path = os.path.join(backup_dir, f) if os.path.isfile(src_path): shutil.copy(src_path, backup_path) print(f"{f} ãããã¯ã¢ãããã¾ãã") # 使ç¨ä¾ src = '.' backup = 'temp' backup_files(src, backup)
ã·ã¹ãã æ å ±ã®åå¾
ã·ã¹ãã ã«é¢ããæ å ±ãåå¾ããå°ããªãã¼ã«ãä½æãã¾ãã
import os def print_system_info(): print(f"OSå: {os.name}") print(f"ã«ã¬ã³ããã£ã¬ã¯ããª: {os.getcwd()}") print(f"ãã¼ã ãã£ã¬ã¯ããª: {os.getenv('HOME')}") print_system_info()
ã¾ã¨ã
os
ã¢ã¸ã¥ã¼ã«ã使ãã¨ãPythonã¹ã¯ãªããããç°¡åã«ã·ã¹ãã æä½ãå¯è½ã§ããæ¬è¨äºã§ã¯ä»¥ä¸ãå¦ã³ã¾ããã
os
ã¢ã¸ã¥ã¼ã«ã®æ¦è¦ã¨åºæ¬çãªä½¿ãæ¹- ãã¡ã¤ã«æä½ããã£ã¬ã¯ããªæä½ãç°å¢å¤æ°ã®æä½
- å®ç¨çãªä¾ãéãã
os
ã¢ã¸ã¥ã¼ã«ã®å¿ç¨æ¹æ³
ãã以å¤ã«ãos
ã¢ã¸ã¥ã¼ã«ã¯å¤ãã®æ©è½ãæã£ã¦ãã¾ãã®ã§ãOSã®æ©è½ã使ãããã¨ãã¯ãã¾ãâos`ã¢ã¸ã¥ã¼ã«ã®ããã¥ã¡ã³ãã調ã¹ã¦ãã ããã