生活随笔
收集整理的這篇文章主要介紹了
[PY3]——IO——文件目录操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
IO—os、shutil—文件目錄操作
?
目錄操作
1. 新建
| os.mkdir(path[, mode]) | 相當于mkdir,新建目錄 |
| os.makedirs(path[, mode]) | 相當于mkdir -p |
os.path.exists("/test2")
False
# os.mkdir( ) 如果父路徑不存在,則新建報錯
os.mkdir('/test2/abc')
FileNotFoundError: [Errno 2] No such file or directory: '/test2/abc'# os.makedirs( ) 會自動創建父目錄,相當于mkdir -p
os.makedirs('/test2/abc')
print(os.path.exists('/test2/abc'))
True ?
2. 刪除
| os.rmdir(path) | 刪除空目錄 |
| shutil.rmtree(path) | 刪除且可以遞歸刪除 |
os.rmdir(
"/test2/abc")
OSError: [Errno 39] Directory not empty: '/test2/abc' ?
3. 查看目錄內容
| os.listdir(path) | 列出目錄下的內容,相當于ls |
# 列出目錄內容但不遞歸子目錄,返回列表
print(os.listdir(
"/test2/abc"))
['abc2', 'kong.txt'] ?
4. 工作目錄的查看和切換
| os.getcwd() | 獲取當前所在目錄 ?相當于pwd |
| os.chdir(path) | 進入指定路徑目錄 ?相當于cd |
print(os.getcwd())
/py3os.chdir('/test')
print(os.getcwd())
/test ?
5. 重命名
| os.rename(src, dst) | 移動/重命名,相當于mv |
print(os.listdir(
"/test2/aa"))
['aa.sh']
print(os.listdir(
"/test2/bb"))
[]os.rename("/test2/aa/aa.sh",
"/test2/bb/aa.sh.bak")print(os.listdir(
"/test2/aa"))
[]
print(os.listdir(
"/test2/bb"))
['aa.sh.bak'] ?
6. 權限
| os.chmod(path, mode) | 修改權限 |
| os.chown(path, uid, gid) | 修改屬主屬組 |
| shutil (path,user=None,group=None) | 修改屬主屬組 |
| os.lchmod(path, mode) | ? |
| os.lchown(path, uid, gid) | ? |
# os.chmod
print(oct(os.stat(
"/test2/").st_mode)[-4
:])
0755
os.chmod("/test2",0o644)print(oct(os.stat(
"/test2/").st_mode)[-4
:])
0644
# os.chown
print(pwd.getpwuid(os.stat(
"/test2").st_uid).pw_name)
root
os.chown("/test2",uname_uid(
"user00"),gname_gid(
"user00"))print(pwd.getpwuid(os.stat(
"/test2").st_uid).pw_name)
user00
print(grp.getgrgid(os.stat(
"/test2").st_gid).gr_name)
user00
# shutil.chown(這種方法的uid、gid參數可以接受字符串,更方便!)
print(pwd.getpwuid(os.stat("/test2/aa.txt").st_uid).pw_name)
root shutil.chown("/test2/aa.txt","user00","user00")
print(pwd.getpwuid(os.stat("/test2/aa.txt").st_uid).pw_name)
user00 ?
7. 復制和移動?
| shutil.copytree | 復制整個目錄樹 |
| shutil.move | 相當于windows下的Ctrl+X、Ctrl+V |
# shutil.move(src,dst,copy_function = copy2 )
print(os.path.exists(
"/test2/aa"))
Trueprint(os.listdir(
"/test2/aa"))
['aa_aa', 'test.txt']
shutil.move("/test2/aa",
"/test2/bb")print(os.path.exists(
"/test2/aa"))
Falseprint(os.path.exists(
"/test2/bb"))
True
print(os.listdir(
"/test2/bb"))
['aa_aa', 'test.txt']# shutil.copytree(src,dst,symlinks = False,ignore = None,copy_function = copy2,ignore_dangling_symlinks = False )
print(os.path.exists(
"/test2/bb"))
Trueprint(os.listdir(
"/test2/bb"))
['aa_aa', 'test.txt']
shutil.copytree("/test2/bb",
"/test/bb",ignore=ignore_patterns(
"*.txt"))print(os.path.exists(
"/test/bb"))
Trueprint(os.listdir(
"/test/bb"))
['aa_aa'] ?
8. 獲取目錄信息
| os.stat(path) | 獲取目錄信息 相當于stat |
| os.statvfs(path) | 獲取指定路徑的文件系統統計信息 |
| os.path.getatime(path) | ? |
| os.path.getmtim(path) | ? |
| os.path.getctime(path) | ? |
| os.path.getsize(path) | ? |
# os.stat輸出的信息和stat命令一致
print(os.stat(
"/test2"))
os.stat_result(st_mode=16804, st_ino=296353, st_dev=2053, st_nlink=4, st_uid=500, st_gid=500, st_size=4096, st_atime=1503497751, st_mtime=1503497749, st_ctime=1503497749)os.system("stat /test2")
File: `/test2'Size: 4096 Blocks: 8 IO Block: 4096 directoryDevice: 805h/2053d Inode: 296353 Links: 4Access: (0644/drw-r--r--) Uid: ( 500/ user00) Gid: ( 500/ user00)Access: 2017-08-23 10:15:51.125760987 -0400Modify: 2017-08-23 10:15:49.496761986 -0400Change: 2017-08-23 10:15:49.496761986 -0400# os.stat中輸出的每一條信息都可以通過 . 的方式單獨獲取
print(os.stat(
"/test2").st_dev)
2053# 獲取ctime、atime、mtime時間(os.stat的輸出是時間戳的形式所以用datetime)
print(datetime.fromtimestamp(os.stat(
"/test2/").st_ctime))
2017-08-22 14:51:07.943986# 獲取uid、gid(os.stat的輸出是gid和uid可以利用pwd和grp轉換為更易讀的username和groupname)
print(pwd.getpwuid(os.stat(
"/test2").st_uid).pw_name)
user00
print(grp.getgrgid(os.stat(
"/test2").st_gid).gr_name)
user00print(os.statvfs(
"/test2"))
os.statvfs_result(f_bsize=4096, f_frsize=4096, f_blocks=2396548, f_bfree=1024591, f_bavail=902851, f_files=609600, f_ffree=384157, f_favail=384157, f_flag=4096, f_namemax=255)f_bsize: 文件系統塊大小
f_frsize: 分棧大小
f_blocks: 文件系統數據塊總數
f_bfree: 可用塊數
f_bavail:非超級用戶可獲取的塊數
f_files: 文件結點總數
f_ffree: 可用文件結點數
f_favail: 非超級用戶的可用文件結點數
f_fsid: 文件系統標識 ID
f_flag: 掛載標記
f_namemax: 最大文件長度 ?
9. 修改
| os.utime(path, times) | 修改文件的訪問和修改的時間 |
?
?
文件操作
1. 新建
f=open(
"/test2/newfile.txt",mode=
"w+")
?
2. 讀寫
(參考《[PY3]——IO——文件讀寫》)
?
3. 刪除
print(os.path.exists(
"/test2/newfile.txt"))
True
os.remove("/test2/newfile.txt")print(os.path.exists(
"/test2/newfile.txt"))
False ?
4. 重命名
同目錄操作
?
5. 權限
同目錄操作
?
6. 移動
| shutil.copyfile | 復制內容,屬主屬組不復制 |
| shutil.copymode | 復制權限 |
| shutil.copystat | 復制元數據 |
| shutil.copy | 復制內容和權限,相當于copyfile+copymode |
| shutil.copy2 | 復制內容和元數據,相當于copyfile+copystat |
?
7. 獲取文件信息
同目錄操作
?
?
其他操作
1. 判斷
| os.path.exists(path) | 判斷該路徑是否存在 |
| os.path.isfile(path) | 判斷是否是文件 |
| os.path.isdir(path) | 判斷是否是目錄 |
| os.path.ismount(path) | 判斷是否是掛載點 |
| os.path.islink(path) | 判斷是否是軟鏈接 |
| os.path.isabs(path) | 判斷是否是絕對路徑 |
?
2. 路徑獲取
| os.path.basename(path) | ? |
| os.path.dirname(path) | ? |
| os.path.relpath(path) | ? |
| os.path.split(path) | 將path分割成目錄和文件名的二元組返回 |
?
3. 軟/硬鏈接
| os.link(src,dst) | ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? |
| os.symlink(src,dst) | ? |
| os.readlink(path) | ? |
?
參考資料
《shutil模塊介紹》
《os.path官方文檔》
《shutil官方文檔》
《python3-os方法總結》
轉載于:https://www.cnblogs.com/snsdzjlz320/p/7410955.html
總結
以上是生活随笔為你收集整理的[PY3]——IO——文件目录操作的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。