python os函数_python os模块主要函数
使用python提供的os模塊,對文件和目錄進行操作,重命名文件,添加,刪除,復制目錄以及文件等。
一、文件目錄常用函數
在進行文件和目錄操作時,一般會用到以下幾種操作。
1、獲得當前;路徑
在python中可以使用os.getcwd()函數獲得當前的路徑。
os.getcwd()
'''幫助文檔:Return a unicode string representing the current working directory.'''
該函數不需要傳遞參數,它返回當前的目錄。需要說明的是,當前目錄并不是之腳本所在的目錄,而是所運行腳本的目錄。
修改當前腳本的目錄使用os.chdir()函數
os.chdir()
'''幫助文檔:chdir(path)
Change the current working directory to the specified path.
path may always be specified as a string.
On some platforms, path may also be specified as an open file descriptor.
If this functionality is unavailable, using it raises an exception.'''
2、獲得目錄中的內容
在python中可以使用os.listdir()函數獲得指定目錄中的內容。
os.listdir(path)
''' 幫助文檔:listdir(path=None)
Return a list containing the names of the files in the directory.
path can be specified as either str, bytes, or a path-like object. 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='.'.
On some platforms, path may also be specified as an open file descriptor;\
the file descriptor must refer to a directory.
If this functionality is unavailable, using it raises NotImplementedError.
The list is in arbitrary order. It does not include the special
entries '.' and '..' even if they are present in the directory.'''
path參數:要獲得內容目錄的路徑
3、創建目錄
在python中可以使用os.mkdir()函數創建目錄。
os.mkdir(path)
'''幫助文檔mkdir(path, mode=511, *, dir_fd=None)
Create a directory.
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.
The mode argument is ignored on Windows.'''
path參數:要創建目錄的路徑
os.mkdir()函數只能創建一個目錄,也就是說路徑參數除了最后面要創建的那個目錄不存在,路徑之前的所有目錄必須存在否則就會出現錯誤,并且當這個目錄存在時也會出現錯誤“當文件已存在,無法創建該文件”
為此我們可以使用另一個函數os.makedirs()函數來創建多級空目錄
os.makedirs(path)
'''幫助文檔:makedirs(name, mode=511, exist_ok=False)
makedirs(name [, mode=0o777][, exist_ok=False])
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.'''
path參數:要創建的多級空目錄,最后一個目錄必須不存在,否則會產生錯誤”當文件已存在,無法創建該目錄“
4、刪除目錄
在python中可以使用os.rmdir()函數刪除目錄
os.rmdir(path)
'''幫助文檔:rmdir(path, *, dir_fd=None)
Remove a directory.
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.'''
path參數:要刪除的目錄的路徑,要刪除的目錄必須是空目錄否則會產生錯誤”目錄不是空的“
為此我們可以使用另一個函數os.removedirs()來刪除多級空目錄,目錄必須為空否則會產生錯誤”目錄不是空的“
5、判斷是否是目錄
在python中可以使用os.path.isdir()函數判斷某一路徑是否為目錄。
os.path.isdir(path)
'''幫助文檔:_isdir(path, /)
Return true if the pathname refers to an existing directory.'''
path參數:要進行判斷的路徑
6、判斷是否為文件
在python中可以使用os.path.isfile()函數判斷某一路徑是否為文件。
os.path.isfile(path)
'''幫助文檔:isfile(path)
Test whether a path is a regular file'''
path參數:要進行判斷的路徑
7、判斷是否是絕對路徑
os.path.isabs()函數判斷路徑是否是絕對路徑
os.path.isabs(path)
'''幫助文檔:isabs(s)
Test whether a path is absolute'''
path參數:要判斷的的路徑
8、檢驗路徑是否真的存在
os.path.exists()函數判斷路徑是否真的存在
os.path.exists(path)
'''幫助文檔:exists(path)
Test whether a path exists. Returns False for broken symbolic links'''
path參數:要判斷的路徑
9、分離路徑與文件名
os.path.split(path)
'''幫組文檔:split(p)
Split a pathname.
Return tuple (head, tail) where tail is everything after the final slash.
Either part may be empty.'''
path參數:要進行分離的路徑
10、分離文件擴展名
os.path.splitext(path)
'''幫助文檔:splitext(p)
Split the extension from a pathname.
Extension is everything from the last dot to the end, ignoring
leading dots. Returns "(root, ext)"; ext may be empty.'''
path參數:要進行分離的路徑
11、獲取路徑名
os.path.dirname(filename)通過文件的路徑只獲取路徑名
os.path.dirname(filename)
'''幫助文檔:dirname(p)
Returns the directory component of a pathname'''
path參數:文件的具體路徑
12、獲取文件名
os.path.basename(filename)通過路徑獲取文件名
os.path.basename(filename)
'''幫助文檔:basename(p)
Returns the final component of a pathname'''
path參數:文件的具體路徑
13、讀取和設置環境變量
os.getenv()函數獲取環境變量os.putenv()設置環境變量
os.getenv()
'''幫助文檔:getenv(key, default=None)
Get an environment variable, return None if it doesn't exist.
The optional second argument can specify an alternate default.
key, default and the result are str.'''
os.putenv()
'''幫助文檔:putenv(name, value, /)
Change or add an environment variable.'''
14、給出當前平臺使用的行終止符
15、指示你正在使用的平臺
os.name()函數指示你當前使用的平臺
windows平臺為’nt‘
16、獲取文件屬性
os.stat(file)函數獲取文件的屬性
os.stat()
'''幫助文檔:stat(path, *, dir_fd=None, follow_symlinks=True)
Perform a stat system call on the given path.
path
Path to be examined; can be string, bytes, a path-like object or
open-file-descriptor int.
dir_fd
If not None, it should be a file descriptor open to a directory,
and path should be a relative string; path will then be relative to
that directory.
follow_symlinks
If False, and the last element of the path is a symbolic link,
stat will examine the symbolic link itself instead of the file
the link points to.
dir_fd and follow_symlinks may not be implemented
on your platform. If they are unavailable, using them will raise a
NotImplementedError.'''
17、重命名文件或者目錄
os.rename(old, new)函數重命名文件或者目錄
os.rename(old, new)
'''幫助文檔:rename(src, dst, *, src_dir_fd=None, dst_dir_fd=None)
Rename a file or directory.
If either src_dir_fd or dst_dir_fd is not None, it should be a file
descriptor open to a directory, and the respective path string (src or dst)
should be relative; the path will then be relative to that directory.
src_dir_fd and dst_dir_fd, may not be implemented on your platform.
If they are unavailable, using them will raise a NotImplementedError.'''
18、獲得文件大小
os.path.getsize()
'''幫助文檔:getsize(filename)
Return the size of a file, reported by os.stat().'''
總結
以上是生活随笔為你收集整理的python os函数_python os模块主要函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: django 修改html无变化,Dja
- 下一篇: python冒泡算法_python_冒泡