python osモジュールについて


1.紹介:osモジュールはpythonファイルとオペレーティングシステムのインタラクションに使用する.osモジュール関数(1)
os.path.abspath(path) Return a normalized absolutized version of the pathname path. On most platforms, this is equivalent to calling the function normpath() as follows: normpath(join(os.getcwd(), path)). 正規化された絶対パスを返します.ほとんどのプラットフォームでは、呼び出し関数normpath()に相当します.
print(os.path.abspath(__file__))
#D:\pythonfiles\exercise\ftp\ftp_server\src\sign.py

(2)
os.path.dirname(path) Return the directory name of pathname path. This is the first element of the pair returned by passing path to the function split(). パスのディレクトリ名を返します.これはosです.split()関数の戻り値の最初の要素
BaseDir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
print(BaseDir)
#D:\pythonfiles\exercise\ftp\ftp_server

(3)
os.path.join(path, *paths) 1)Join one or more path components intelligently. The return value is the concatenation of path and any members of *paths with exactly one directory separator (os.sep) following each non-empty part except the last, meaning that the result will only end in a separator if the last part is empty. If a component is an absolute path, all previous components are thrown away and joining continues from the absolute path component. 2)On Windows, the drive letter is not reset when an absolute path component (e.g., r’\foo’) is encountered. If a component contains a drive letter, all previous components are thrown away and the drive letter is reset. Note that since there is a current directory for each drive, os.path.join(“c:”, “foo”) represents a path relative to the current directory on drive C: (c:foo), not c:\foo.
インテリジェントに1つ以上のパスを接続する、戻り値はpathと*pathの直列パスであり、最後のパスを除いて、各非空パラメータ間に1つのパス分割子で接続する、最後のパラメータが空であれば、最終的な戻りパスはパス分割子で終わる.また、パスのうちの1つが絶対パスである場合、その前のパラメータはすべて破棄され、この絶対パスからパッチが開始する.
Windowsシステムでは、絶対パスに遭遇するとドライブシンボルはリセットされないが、パスにドライブシンボルが含まれていると、前のパスが破棄され、ドライブがリセットする.また、ディスクが直接ディレクトリに接続する場合:os.path.join(「d:」,「foo」)はd:fooを返す
使用例:
path = os.path.join(BaseDir, 'conf', 'user_info.conf')
print(path)  #      
#D:\pythonfiles\exercise\ftp\ftp_server\conf\user_info.conf
path1 = os.path.join(BaseDir, '\\conf', 'user_info.conf', '')
print(path1)  
#          ,         ,       ,        ,              
#D:\conf\user_info.conf\

(4)
os.listdir(path=’.’) 1)Return a list containing the names of the entries in the directory given by path. The list is in arbitrary order, and does not include the special entries ‘.’ and ‘…’ even if they are present in the directory. 2)path may be a path-like object. If path is of type bytes (directly or indirectly through the PathLike interface), the filenames returned will also be of type bytes; in all other circumstances, they will be of type str. 3)This function can also support specifying a file descriptor; the file descriptor must refer to a directory. Note: To encode str filenames to bytes, use fsencode(). See also: The scandir() function returns directory entries along with file attribute information, giving better performance for many common use cases.
パスディレクトリの下にあるファイル名を含むリストを返します.リストの順序は任意に並べられており、'.と入り口;pathがbytes形式である場合、返されるファイル名もbytes形式である.OSを使用する場合.scandir()はより良い表現があるかもしれません.残りは補充する
print(os.listdir(os.path.dirname(os.path.abspath(__file__))))
print(os.listdir())  #        
#['test.py', 'user_info.conf']
#['test.py', 'user_info.conf']

os.scandirの使用例:
a = os.scandir()  #     os.DirEntry      ,path   ' . '
print(a)  #     next()  ???
for i in a:  #     
    print(i.name)
    print(i.path)  #           os.scandir        
    print(i.inode())
    print(i.is_file(), i.is_dir())
    print(i.stat())
#result
<nt.ScandirIterator object at 0x00000112AC3D0270>
test.py
.\test.py
9288674231720971
True False
os.stat_result(st_mode=33206, st_ino=0, st_dev=0, st_nlink=0, st_uid=0, st_gid=0, st_size=868, st_atime=1541654991, st_mtime=1541654991, st_ctime=1541654991)
user_info.conf
.\user_info.conf
3377699720797180
True False
os.stat_result(st_mode=33206, st_ino=0, st_dev=0, st_nlink=0, st_uid=0, st_gid=0, st_size=149, st_atime=1541646585, st_mtime=1541642663, st_ctime=1541642663)

(5)
os.chdir(path) Change the current working directory to path. This function can support specifying a file descriptor. The descriptor must refer to an opened directory, not an open file. New in version 3.3: Added support for specifying path as a file descriptor on some platforms.
現在の作業ディレクトリを変更
(6)
os.path.exists(path) Return True if path refers to an existing path or an open file descriptor. Returns False for broken symbolic links. On some platforms, this function may return False if permission is not granted to execute os.stat() on the requested file, even if the path physically exists.
(7)
os.path.isfile(path) Return True if path is an existing regular file. This follows symbolic links, so both islink() and isfile() can be true for the same path.
os.path.isdir(path) Return True if path is an existing directory. This follows symbolic links, so both islink() and isdir() can be true for the same path.