【學習】python(os)模塊總結
阿新 • • 發佈:2017-10-12
oca python shell link pyc sts osi 模塊 ech
Python OS模塊實驗總結
1、os.name(返回正在使用的平臺,Linux顯示為"posix",Windows顯示為"nt")
>>> os.name ‘posix‘
2、os.getcwd() (顯示當前操作目錄)
>>> os.getcwd() ‘/root/test‘
3、os.listdir()(列出指定目錄下面的所有目錄和文件)
>>> os.listdir("/root/test") [‘test.py‘, ‘tab.py‘, ‘aa‘, ‘bb‘, ‘cc‘, ‘tab.pyc‘]
4、os.remove() (刪除目錄下的指定文件)
>>> os.remove("./test.py") >>> os.listdir("/root/test") [‘tab.py‘, ‘aa‘, ‘bb‘, ‘cc‘, ‘tab.pyc‘]
5、os.mkdir()(創建目錄)
>>> os.mkdir("mkdir") >>> os.listdir(".") [‘tab.py‘, ‘aa‘, ‘bb‘, ‘cc‘, ‘tab.pyc‘, ‘mkdir‘]
6、os.rmdir()(刪除指定目錄)
>>> os.rmdir("mkdir") >>> os.listdir(".") [‘tab.py‘, ‘aa‘, ‘bb‘, ‘cc‘, ‘tab.pyc‘]
7、os.path.isabs() (判斷是否是絕對路徑)
os.path.isdir() (判斷是否是目錄)
os.path.isfile()(判斷是否是文件)
os.path.islink()(判斷是否是軟鏈接)
os.path.ismount()(判斷目錄是否是掛載點)
>>> os.path.isfile("tab.py") True >>> os.path.islink("/etc/localtime") True >>> os.path.isabs("/root/") True >>> os.path.ismount("/") True
8、os.path.exists() (判斷對象是否存在)
>>> os.path.exists("/root") True
9、os.path.split()(返回對象的目錄和文件名)
>>> os.path.split("/root/test/tab.py") (‘/root/test‘, ‘tab.py‘)
10、os.system()(執行shell命令並返回結果)
>>> os.system("echo ‘Hello World!‘") Hello World!
11、os.chdir()(切換當前目錄)
>>> os.chdir("/opt") >>> os.getcwd() ‘/opt‘
12、os.path.join()(拼接目錄和文件名)
>>> os.path.join("/root/","test.py") ‘/root/test.py‘
13、os.rename()(重命名文件)
>>> os.rename("project1","project11")
【學習】python(os)模塊總結