osのpython共通モジュール
2022-03-02 03:36:17
osのpython共通モジュール
OSモジュールは、pythonがオペレーティングシステムと対話するためのインターフェイスです。
OSモジュールのメソッド
os.getcwd() gets the current working directory, i.e. the path to the directory where the current python script is working
os.chdir("dirname") changes the current working directory of the script; equivalent to cd in the shell
os.curdir returns the current directory: ('.')
os.pardir gets the parent directory string name of the current directory: ('...')
os.makedirs('dirname1/dirname2') generates multiple recursive directories
os.removedirs('dirname1') If the directory is empty, delete it and recurse to the previous level, if it is also empty, delete it, and so on
os.mkdir('dirname') generates a single-level directory; equivalent to mkdir dirname in the shell
os.rmdir('dirname') Delete a single-level empty directory; if the directory is not empty, it cannot be deleted and an error is reported; equivalent to rmdir dirname in the shell
os.listdir('dirname') lists all files and subdirectories in the specified directory, including hidden files, and prints them as a list
os.remove() removes a file
os.rename("oldname","newname") renames a file/directory
os.stat('path/filename') Get file/directory information
os.sep Outputs OS-specific path separators, "\\\" for Win, "/" for Linux;
os.linesep outputs the line terminator used by the current platform, "\t\n" under Win, "\n" under Linux, "\n"
os.pathsep Outputs the string used to split the file path:
os.name Outputs a string indicating the current platform. win->'nt'; Linux->'posix'
os.system("bash command") Runs a shell command and displays it directly
os.environ Get system environment variables
os.path.abspath(path) returns the path normalized absolute path
os.path.split(path) splits path into a directory and filename binary and returns
os.path.dirname(path) returns the directory of path. This is actually the first element of os.path.split(path)
os.path.basename(path) returns the last filename of path. If path ends with / or \, then it returns null. That is, the second element of os.path.split(path)
os.path.exists(path) returns True if path exists, False if path does not exist
os.path.isabs(path) returns True if path is an absolute path
os.path.isfile(path) Returns True if path is an existing file, False otherwise
os.path.isdir(path) Returns True if path is an existing directory. otherwise returns False
os.path.join(path1[, path2[, ...]]) Returns multiple paths combined, with arguments before the first absolute path being ignored
os.path.getatime(path) returns the last access time of the file or directory pointed to by path
os.path.getmtime(path) returns the last modification time of the file or directory pointed to by path
os.path.getsize(path) returns the size of the path
#! /usr/bin/env python3
# -*- coding:utf-8 -*-
import os
# List all files and subdirectories in the specified directory, including hidden files, and return them as a list
res = os.listdir(r'E:/')
print(res)
# Count the size of the file, return the value in bytes
size = os.path.getsize(r'03.os module.py')
print(size)
# os.remove() Remove a file
os.remove(r'tmp')
# os.rename("oldname","newname") rename file/directory
os.rename('text', 'text1')
# os.system("bash command") run shell command, display directly
os.system(r'dir e:')
# os.environ Get system environment variables
print(os.environ)
# os.path.dirname(path) Returns the directory of path
print(os.path.dirname('03.os module.py'))
# os.path.basename(path) returns the last file name of path
print(os.path.basename('03.os module.py'))
# os.path.exists(path) Returns True if path exists, False if path does not exist
print(os.path.exists('03.os module.py'))
# os.path.join(path1[, path2[, ...]]) Combine multiple paths and return them, the arguments before the first absolute path are ignored
print(os.path.join('a', 'b', 'c'))
OSモジュールの共通メソッド
['$RECYCLE.BIN', 'Application', 'Config.Msi', 'Develop_tools', 'System Volume Information']
1129
The volume in drive E is Tools
The volume's serial number is F31A-8533
The directory for E:\ is
2021/01/09 18:10 <DIR> Application
2020/12/30 13:15 <DIR> Develop_tools
0 files 0 bytes
2 directories 97,894,666,240 available bytes
environ({'ALLUSERSPROFILE': 'C:\\ProgramData', 'APPDATA': 'C:\\Users\\\\123\\\AppData\\\\Roaming', 'CLASSPATH': '.C:\Program Files\\\Java\\\ JDK8\\lib\\\\dt.jar;C:\\Program Files\\\Java\\\\JDK8\\\\lib\\\tools.jar', 'COMMONPROGRAMFILES': 'C:\\\Program Files (x86)\\\\Common Files', ' COMMONPROGRAMFILES(X86)': 'C:\\Program Files (x86)\\\Common Files', 'COMMONPROGRAMW6432': 'C:\\Program Files\\\\Common Files', 'COMPUTERNAME': 'LAPTOP-AV5TB0BF', 'COMSPEC': 'C:\\Windows\\system32\\\\cmd.exe', 'DATAGRIP': 'E:\\\Develop_tools\\\\DataGrip 2020.3\\\bin;', 'DRIVERDATA': 'C: \\Windows\\System32\\Drivers\\\DriverData', 'FPS_BROWSER_APP_PROFILE_STRING': 'Internet Explorer', 'FPS_BROWSER_USER_PROFILE_STRING': ' Default', 'HADOOP_HOME': 'D:\\JAVAEE\\\hadoop-2.8.3', 'HOMEDRIVE': 'C:', 'HOMEPATH': '\\Users\\\\123', 'IDEA_INITIAL_DIRECTORY': 'C:\Users\\\\123 123\\\Desktop', 'INTELLIJ IDEA': 'E:\\\Develop_tools\\\IntelliJ IDEA 2020.3\\\bin;', 'JAVA_HOME': 'C:\\\Program Files\\\\Java\\\\\JDK8', ' LOCALAPPDATA': 'C:\\Users\\\\123\\\AppData\\\Local', 'LOGONSERVER': '\\\\LAPTOP-AV5TB0BF', 'NUMBER_OF_PROCESSORS': '8', 'ONEDRIVE': 'C:\\\ Users\\\123\\\OneDrive', 'OS': 'Windows_NT', 'PATH': 'D:\\\Code\\\\PyCode\\\chaney02\\\\venv\\\\Scripts;E:\\\\Develop_tools\\\\Xftp\\;E:Develop_tools tools\\\XShell\\\;C:\\\Program Files (x86)\\\Common Files\\\Oracle\\\\Java\\\javapath;C:\\\Windows\\\\system32;C:\\\Windows;C:\\\Windows\\\\System32 \\\Wbem;C:\\Windows\\\\System32\\\WindowsPowerShell\\\\v1.0\\\;C:\\\Windows\\\\System32\\\\\OpenSSH\\\;C:\\\Program Files (x86)\\\\NVIDIA Corporation\ \PhysX\\\Common;C:\\Program Files\\\\NVIDIA Corporation\\\NVIDIA NvDLISR;C:\\Program Files\\\Java\\\\\JDK8\\\\bin;C:\Program Files\\\Java\\\\JDK8\\\\ jre\\\bin;D:\\JAVAEE\\\\hadoop-2.8.3\\\bin;E:\\\Develop_tools\\\\Python3.7;E:\\\Application\\\\QQ\\\\Bin;E:\\Application\\\\WeChat;E:\\\Develop_tools\\\\\\Python3.7 tools\\\MySQL5.5\\\bin;E:\\Develop_tools\\\Xftp7\\\;E:\\\Develop_tools\\\\XShell\\\;E:\\\Develop_tools\\\\Python3.7\\\\Scripts\\\;E:\\\Develop_tools\\\\\Python3.7\\\Scripts\\\;E:\\\Develop_ tools\\\Python3.7;C:\\Users\\\123\\\AppData\\\Local\\Microsoft\\\\WindowsApps;E:\\\Develop_tools\\\\\PyCharm 2019.3.5\\\\bin;;E:\\\\Develop_tools \\WebStorm 2020.3\\\bin;;E:\\Develop_tools\\\IntelliJ IDEA 2020.3\\\bin;;E:\\Develop_tools\\\\PyCharm 2020.3\\\bin;;E:\Develop_tools\\\\ DataGrip 2020.3\\bin;;', 'PATHEXT': '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC', 'PROCESSOR_ARCHITECTURE': 'x86', 'PROCESSOR _ARCHITEW6432': 'AMD64', 'PROCESSOR_IDENTIFIER': 'Intel64 Family 6 Model 142 Stepping 12, GenuineIntel', 'PROCESSOR_LEVEL': '6', 'PROCESSOR_ REVISION
結果を実行する
['$RECYCLE.BIN', 'Application', 'Config.Msi', 'Develop_tools', 'System Volume Information']
1129
The volume in drive E is Tools
The volume's serial number is F31A-8533
The directory for E:\ is
2021/01/09 18:10 <DIR> Application
2020/12/30 13:15 <DIR> Develop_tools
0 files 0 bytes
2 directories 97,894,666,240 available bytes
environ({'ALLUSERSPROFILE': 'C:\\ProgramData', 'APPDATA': 'C:\\Users\\\\123\\\AppData\\\\Roaming', 'CLASSPATH': '.C:\Program Files\\\Java\\\ JDK8\\lib\\\\dt.jar;C:\\Program Files\\\Java\\\\JDK8\\\\lib\\\tools.jar', 'COMMONPROGRAMFILES': 'C:\\\Program Files (x86)\\\\Common Files', ' COMMONPROGRAMFILES(X86)': 'C:\\Program Files (x86)\\\Common Files', 'COMMONPROGRAMW6432': 'C:\\Program Files\\\\Common Files', 'COMPUTERNAME': 'LAPTOP-AV5TB0BF', 'COMSPEC': 'C:\\Windows\\system32\\\\cmd.exe', 'DATAGRIP': 'E:\\\Develop_tools\\\\DataGrip 2020.3\\\bin;', 'DRIVERDATA': 'C: \\Windows\\System32\\Drivers\\\DriverData', 'FPS_BROWSER_APP_PROFILE_STRING': 'Internet Explorer', 'FPS_BROWSER_USER_PROFILE_STRING': ' Default', 'HADOOP_HOME': 'D:\\JAVAEE\\\hadoop-2.8.3', 'HOMEDRIVE': 'C:', 'HOMEPATH': '\\Users\\\\123', 'IDEA_INITIAL_DIRECTORY': 'C:\Users\\\\123 123\\\Desktop', 'INTELLIJ IDEA': 'E:\\\Develop_tools\\\IntelliJ IDEA 2020.3\\\bin;', 'JAVA_HOME': 'C:\\\Program Files\\\\Java\\\\\JDK8', ' LOCALAPPDATA': 'C:\\Users\\\\123\\\AppData\\\Local', 'LOGONSERVER': '\\\\LAPTOP-AV5TB0BF', 'NUMBER_OF_PROCESSORS': '8', 'ONEDRIVE': 'C:\\\ Users\\\123\\\OneDrive', 'OS': 'Windows_NT', 'PATH': 'D:\\\Code\\\\PyCode\\\chaney02\\\\venv\\\\Scripts;E:\\\\Develop_tools\\\\Xftp\\;E:Develop_tools tools\\\XShell\\\;C:\\\Program Files (x86)\\\Common Files\\\Oracle\\\\Java\\\javapath;C:\\\Windows\\\\system32;C:\\\Windows;C:\\\Windows\\\\System32 \\\Wbem;C:\\Windows\\\\System32\\\WindowsPowerShell\\\\v1.0\\\;C:\\\Windows\\\\System32\\\\\OpenSSH\\\;C:\\\Program Files (x86)\\\\NVIDIA Corporation\ \PhysX\\\Common;C:\\Program Files\\\\NVIDIA Corporation\\\NVIDIA NvDLISR;C:\\Program Files\\\Java\\\\\JDK8\\\\bin;C:\Program Files\\\Java\\\\JDK8\\\\ jre\\\bin;D:\\JAVAEE\\\\hadoop-2.8.3\\\bin;E:\\\Develop_tools\\\\Python3.7;E:\\\Application\\\\QQ\\\\Bin;E:\\Application\\\\WeChat;E:\\\Develop_tools\\\\\\Python3.7 tools\\\MySQL5.5\\\bin;E:\\Develop_tools\\\Xftp7\\\;E:\\\Develop_tools\\\\XShell\\\;E:\\\Develop_tools\\\\Python3.7\\\\Scripts\\\;E:\\\Develop_tools\\\\\Python3.7\\\Scripts\\\;E:\\\Develop_ tools\\\Python3.7;C:\\Users\\\123\\\AppData\\\Local\\Microsoft\\\\WindowsApps;E:\\\Develop_tools\\\\\PyCharm 2019.3.5\\\\bin;;E:\\\\Develop_tools \\WebStorm 2020.3\\\bin;;E:\\Develop_tools\\\IntelliJ IDEA 2020.3\\\bin;;E:\\Develop_tools\\\\PyCharm 2020.3\\\bin;;E:\Develop_tools\\\\ DataGrip 2020.3\\bin;;', 'PATHEXT': '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC', 'PROCESSOR_ARCHITECTURE': 'x86', 'PROCESSOR _ARCHITEW6432': 'AMD64', 'PROCESSOR_IDENTIFIER': 'Intel64 Family 6 Model 142 Stepping 12, GenuineIntel', 'PROCESSOR_LEVEL': '6', 'PROCESSOR_ REVISION
最新
-
nginxです。[emerg] 0.0.0.0:80 への bind() に失敗しました (98: アドレスは既に使用中です)
-
htmlページでギリシャ文字を使うには
-
ピュアhtml+cssでの要素読み込み効果
-
純粋なhtml + cssで五輪を実現するサンプルコード
-
ナビゲーションバー・ドロップダウンメニューのHTML+CSSサンプルコード
-
タイピング効果を実現するピュアhtml+css
-
htmlの選択ボックスのプレースホルダー作成に関する質問
-
html css3 伸縮しない 画像表示効果
-
トップナビゲーションバーメニュー作成用HTML+CSS
-
html+css 実装 サイバーパンク風ボタン
おすすめ
-
ハートビート・エフェクトのためのHTML+CSS
-
HTML ホテル フォームによるフィルタリング
-
HTML+cssのボックスモデル例(円、半円など)「border-radius」使いやすい
-
HTMLテーブルのテーブル分割とマージ(colspan, rowspan)
-
ランダム・ネームドロッパーを実装するためのhtmlサンプルコード
-
Html階層型ボックスシャドウ効果サンプルコード
-
QQの一時的なダイアログボックスをポップアップし、友人を追加せずにオンラインで話す効果を達成する方法
-
sublime / vscodeショートカットHTMLコード生成の実装
-
HTMLページを縮小した後にスクロールバーを表示するサンプルコード
-
html のリストボックス、テキストフィールド、ファイルフィールドのコード例