做技術(shù)一定要一顆恒心,這樣才不會(huì)半途而廢。
目錄

上一節(jié)我們介紹了文件相關(guān)的操作,本節(jié)我們來(lái)介紹目錄相關(guān)的操作。
1,os 模塊
os 模塊是Python 中的內(nèi)建模塊,該模塊中包含許多系統(tǒng)相關(guān)的操作。我們要介紹的目錄相關(guān)的操作,也都包含在該模塊中。
我們可以使用dir(os) 來(lái)查看該模塊中所有屬性和函數(shù),共有幾百個(gè)屬性和函數(shù)。這里我們只介紹一部分函數(shù),可以使用help(os.函數(shù)名) 來(lái)查看某個(gè)函數(shù)的幫助手冊(cè)。
-
os.linesep:獲取當(dāng)前系統(tǒng)的行的換行符。 -
os.getcwd():獲取當(dāng)前工作目錄。 -
os.listdir(path=None):列出path路徑中的所有文件和目錄。path為None時(shí),表示當(dāng)前目錄。 -
os.path.abspath(path):獲取path的絕對(duì)路徑。 -
os.path.isfile(path):判斷path是否是一個(gè)文件。 -
os.path.isdir(path):如果path存在且為目錄,則返回True,否則返回False。 -
os.path.isabs(path):判斷path是否是一個(gè)絕對(duì)路徑。 -
os.path.exists(path):判斷path是否存在。 -
os.path.split(file):返回file的路徑名與文件名,返回值是一個(gè)元組。 -
os.path.splitext(file):返回file的路徑與擴(kuò)展名,返回值是一個(gè)元組。 -
os.path.dirname(path):返回path的目錄 。 -
os.path.basename(path):返回path的文件名。 -
os.rename(old, new): 將文件或目錄old重命名為new。 -
os.mkdir(dir):創(chuàng)建目錄,dir只能是單級(jí)目錄。 -
os.makedirs(dir):創(chuàng)建目錄,dir可以是單級(jí)目錄,也可以是多級(jí)目錄 。 -
os.rmdir(dir):刪除目錄,dir只能是空目錄,否則拋出異常。 -
os.remove(path):刪除文件,如果出現(xiàn)錯(cuò)誤將拋出異常。 -
os.removedirs(path):刪除目錄,且該目錄中不能有其它文件或目錄,也就是該目錄必須為空,否則將出現(xiàn)異常。 -
os.stat(path):獲取文件或目錄的狀態(tài)信息,比如創(chuàng)建時(shí)間,大小等。其返回值為os.stat_result類型。 -
os.path.getsize(file):返回file的大小 。 -
os.chmod(file):修改file的訪問(wèn)權(quán)限。 -
os.chdir(path):從當(dāng)前目錄切換到目錄path。
2,shutil 模塊
shutil 模塊主要是用來(lái)操作文件和目錄的。
我們可以使用help(shutil) 查看該模塊的幫助文檔,使用dir(shutil) 查看其支持的所有類,屬性和方法。
>>> dir(shutil)
['Error', 'ExecError', 'ReadError',
'RegistryError', 'SameFileError',
'SpecialFileError', '_ARCHIVE_FORMATS',
'_BZ2_SUPPORTED', '_LZMA_SUPPORTED',
'_UNPACK_FORMATS', '_ZLIB_SUPPORTED',
'__all__', '__builtins__', '__cached__',
'__doc__', '__file__', '__loader__',
'__name__', '__package__', '__spec__',
'_basename', '_check_unpack_options',
'_copyxattr', '_destinsrc',
'_ensure_directory', '_find_unpack_format',
'_get_gid', '_get_uid', '_make_tarball',
'_make_zipfile', '_ntuple_diskusage',
'_rmtree_safe_fd', '_rmtree_unsafe',
'_samefile', '_unpack_tarfile',
'_unpack_zipfile', '_use_fd_functions',
'chown', 'collections', 'copy', 'copy2',
'copyfile', 'copyfileobj', 'copymode',
'copystat', 'copytree', 'disk_usage',
'errno', 'fnmatch', 'get_archive_formats',
'get_terminal_size', 'get_unpack_formats',
'getgrnam', 'getpwnam', 'ignore_patterns',
'make_archive', 'move', 'os',
'register_archive_format',
'register_unpack_format',
'rmtree', 'stat', 'sys', 'unpack_archive',
'unregister_archive_format',
'unregister_unpack_format', 'which']
這里是該模塊的官方文檔,本節(jié)中我們只介紹少數(shù)幾個(gè)操作,其它函數(shù)的用法,可參考其官方文檔。
-
shutil.copyfile(oldfile, newfile):將文件oldfile復(fù)制一份到newfile。 -
shutil.copy(oldfile, new):將文件oldfile復(fù)制一份到new,返回新的文件名。new可以是文件,也可以是目錄。 -
shutil.copytree(olddir, newdir):將整個(gè)目錄olddir,遞歸拷貝到newdir。 -
shutil.move(src, dst):將src移動(dòng)到dst。 -
shutil.rmtree(dir):刪除整個(gè)目錄樹(shù)dir。
(完。)
推薦閱讀:
Python 簡(jiǎn)明教程 ---20,Python 類中的屬性與方法
Python 簡(jiǎn)明教程 ---21,Python 繼承與多態(tài)
Python 簡(jiǎn)明教程 ---22,Python 閉包與裝飾器
Python 簡(jiǎn)明教程 ---23,Python 異常處理
Python 簡(jiǎn)明教程 ---24,Python 文件讀寫(xiě)