您好,登錄后才能下訂單哦!
Python中OS模塊有哪些常用內置方法?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
OS模塊的常用內置方法
chdir
修改當前工作目錄到指定目錄 Change the current working directory to the specified path.
chmod
修改一個文件的訪問權限 Change the access permissions of a file.
chown
把一個目錄的屬主和屬組修改成另一個數字的屬主和屬組 Change the owner and group id of path to the numeric uid and gid
chroot
修改根目錄(在liunx下使用) Change root directory to path.
ctermid
返回一個進程的控制臺的名字 Return the name of the controlling terminal for this process.
getcwd
以字符串格式顯示當前工作目錄(python解釋器的工作目錄) Return a unicode string representing the current working directory.
getegid
獲取當前進程的有效組ID Return the current process's effective group id.
getenv
獲取系統的環境變量,環境變量不存在時返回None Get an environment variable, return None if it doesn't exist.
geteuid
獲取當前進程的有效用戶ID Return the current process's effective user id.
getgid
獲取當彰進程的組ID Return the current process's group id.
getgrouplist
返回指定用戶的基本組的列表 getgrouplist(user, group) -> list of groups to which a user belongs Returns a list of groups to which a user belongs.
getgroups
獲取一個進程的附加組ID的列表 Return list of supplemental group IDs for the process.
getloadavg
返回當前一個由系統1分鐘,5分鐘和15分鐘的平均負載使用率組成的元組 Return average recent system load information. Return the number of processes in the system run queue averaged over the last 1, 5, and 15 minutes as a tuple of three floats. Raises OSError if the load average was unobtainable.
getlogin
返回當前系統的實際登陸的用戶名 Return the actual login name.
getpid
返回當前的進程ID號 Return the current process id.
getppid
返回當前進程的父進程的ID號 Return the parent's process id.
getuid
返回當前進程的用戶ID Return the current process's user id.
kill
結束一個進程 Kill a process with a signal.
killpg
結束掉一個進程組 Kill a process group with a signal.
listdir
返回指定路徑下由所有所有文件的名字組成的列表,未指定路徑則默認為當前路徑 Return a list containing the names of the files in the directory. path can be specified as either str or bytes. If path is bytes, the filenames returned will also be bytes; in all other circumstances the filenames returned will be str. If path is None, uses the path='.'.
lseek
設置并返回指定文件的指針的位置, Set the position of a file descriptor. Return the new position. Return the new cursor position in number of bytes relative to the beginning of the file.
major
獲取一個設備文件的主設備號 Extracts a device major number from a raw device number.
makedev
在系統上創建一個新的設備文件 Composes a raw device number from the major and minor device numbers.
makedirs
遞歸創建多層目錄,如果目標目錄已經存在,則報錯 Super-mkdir; create a leaf directory and all intermediate ones. Works like mkdir, except that any intermediate path segment (not just the rightmost) will be created if it does not exist. If the target directory already exists, raise an OSError if exist_ok is False. Otherwise no exception is raised. This is recursive.
minor
獲取一個設備文件的次設備號 Extracts a device minor number from a raw device number.
mkdir
創建一個目錄 Create a directory.
mkfifo
創建一個管道文件 If dir_fd is not None, it should be a file descriptor open to a directory, and path should be relative; path will then be relative to that directory. dir_fd may not be implemented on your platform. If it is unavailable, using it will raise a NotImplementedError.
nice
為一個進程增加屬性,也即修改一個進程的nice值 Add increment to the priority of process and return the new priority.
open
在硬件層面打開文件,返回一個整型的文件描述符 Open a file for low level IO. Returns a file descriptor (integer).
putenv
修改或增加一個環境變量 Change or add an environment variable.
read
讀取指定文件的文件描述符,返回字節對象 Read from a file descriptor. Returns a bytes object.
remove
刪除指定文件 Remove a file (same as unlink()).
removedirs
刪除多層目錄, Super-rmdir; remove a leaf directory and all empty intermediate ones. Works like rmdir except that, if the leaf directory is successfully removed, directories corresponding to rightmost path segments will be pruned away until either the whole path is consumed or an error occurs. Errors during this latter phase are ignored -- they generally mean that a directory was not empty.
rename
為一個文件或目錄重命名 Rename a file or directory.
rmdir
刪除一個目錄 Remove a directory.
setegid
設置當前進程的有效組ID Set the current process's effective group id.
seteuid
設置當前進程的有效用戶ID Set the current process's effective user id.
setgid
設置當前進程的組ID Set the current process's group id.
setuid
為當前進程設置用戶ID Set the current process's user id.
stat
顯示指定路徑的詳細信息 Perform a stat system call on the given path.
strerror
把錯誤號碼轉換成信息字符串 Translate an error code to a message string.
symlink
為指定目錄創建軟鏈接 symlink(src, dst, target_is_directory=False, *, dir_fd=None) Create a symbolic link pointing to src named dst.
sync
強制寫入內容到磁盤中 Force write of everything to disk.
system
在子shell中執行系統命令 Execute the command in a subshell.
times
返回系統時間信息的集合 Return a collection containing process timing information.
umask
為文件或目錄設置并返回新權限模式 Set the current numeric umask and return the previous umask.
uname
返回當前操作系統的內核信息 Return an object identifying the current operating system.
unlink
刪除一個軟鏈接 unlink(path, *, dir_fd=None) Remove a file (same as remove()).
unsetenv
刪除一個環境變量 Delete an environment variable.
utime
為指定路徑設置最后訪問時間和最后修改時間 utime(path, times=None, *, ns=None, dir_fd=None, follow_symlinks=True) Set the access and modified time of path.
walk
給定目錄名,返回一個由當前目錄名,當前目錄的子目錄以及當前目錄下的普通文件組成的元組 Directory tree generator. For each directory in the directory tree rooted at top (including top itself, but excluding '.' and '..'), yields a 3-tuple dirpath, dirnames, filenames dirpath is a string, the path to the directory. dirnames is a list of the names of the subdirectories in dirpath (excluding '.' and '..'). filenames is a list of the names of the non-directory files in dirpath. Note that the names in the lists are just names, with no path components. To get a full path (which begins with top) to a file or directory in dirpath, do os.path.join(dirpath, name).
關于Python中OS模塊有哪些常用內置方法問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。