'''
os.environ 獲取系統的環境變量
os.name nt -- windows \r\n | posix --- Linux \nos.path:'''
import osprint(os.environ)
print(os.environ['OS'])print(os.path.abspath('t1/file01.py')) # 獲取絕對路徑
print(os.path.isabs('t1/file01.py')) # 判斷所給的路徑是否是一個絕對路徑
print(os.path.isfile('t1/file01.py')) # True 判斷是否是文件
print(os.path.isdir('t1/file01.py')) # False 判斷是否是文件夾
print(os.path.exists('t1/file02.py')) # False 判斷是否存在文件夾或者文件
print(os.path.join(r'c:\foo', 'a.txt'))
print(os.path.split(r'c:\foo\a.txt'))path = r'C:\考試1\chen\yuan\post bar\a.png'
print(os.path.split(path))print(os.path.getsize(r'C:\images\desk_background.jpg')) # 單位字節
# 5*1024*1024 1Mprint(os.path.getatime(r'C:\images\desk_background.jpg')) # 訪問時間
print(os.path.getctime(r'C:\images\desk_background.jpg')) # 創建時間 windows
print(os.path.getmtime(r'C:\images\desk_background.jpg')) # 修改時間# os.remove()
# path = 't2'
# if os.path.isdir(path):
# files = os.listdir(path)
# if len(files) == 0:
# os.rmdir('t1') # 刪除文件夾
# else:
# for file in files:
# path1 = os.path.join(path, file)
# os.remove(path1) # 刪除文件# 也可以遞歸刪除文件
# def delAll(path):
# if os.path.isdir(path):
# files = os.listdir(path) # ['a.doc', 'b.xls', 'c.ppt']
# # 遍歷并刪除文件
# for file in files:
# p = os.path.join(path, file)
# if os.path.isdir(p):
# # 遞歸
# delAll(p)
# else:
# os.remove(p)
# # 刪除文件夾
# os.rmdir(path)
# else:
# os.remove(path)
#
#
# delAll('c:/foo')
'''
os
mkdir()
rmdir() 空的文件夾
非空: OSError: [WinError 145] 目錄不是空的。: 'c:/考試1'
遞歸的方式
import shutil
shutil.rmtree(r'C:\bank_system') 非空文件夾的刪除os.listdir(path) 查看path下的內容,并以列表的形式返回
os.chdir('c:/考試1') 切換目錄
os.getcwd() 獲取當前文件的路徑 (絕對路徑)os.getpid() get process id 獲取當前的進程id
os.getppid() get parent process id 獲取父進程id'''
import osprint(os.name)
# try:
# # os.mkdir('t2')
# os.mkdir('c:/foo')
# except FileExistsError:
# print('文件夾已經存在')
# os.rmdir('c:/foo')
# os.rmdir('c:/考試1')# import shutil
#
# shutil.rmtree(r'C:\bank_system')files = os.listdir(r'C:\online-store')
print(files)print(os.getpid())
print(os.getppid())print(os.getcwd())
os.chdir('c:/考試1') # change dir
print(os.getcwd())
list1 = os.listdir(os.getcwd())
print(list1)# 也可以遞歸刪除文件
def delAll(path):if os.path.isdir(path):files = os.listdir(path) # ['a.doc', 'b.xls', 'c.ppt']# 遍歷并刪除文件for file in files:p = os.path.join(path, file)if os.path.isdir(p):# 遞歸delAll(p)else:os.remove(p)# 刪除文件夾os.rmdir(path)else:os.remove(path)
?