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.name()

この関数は、インポートするオペレーティング システム モジュールの名前を提供します。

平均と平均

現在、「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() 現在の作業ディレクトリを変更する関数。

 import os os.chdir('d:\') 

出力:

 d:\ 

os.rmdir()

rmdir() 関数は、絶対パスまたは関連パスを使用して指定されたディレクトリを削除します。まず、現在の作業ディレクトリを変更し、フォルダーを削除する必要があります。

C++での継承

タイプスクリプトでのマップ
 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.error()

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.rename()

ファイルまたはディレクトリの名前は、関数を使用して変更できます。 os.rename() 。ファイルを変更する権限があるユーザーは、ファイルの名前を変更できます。

 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.access()

この関数は実数を使用します 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