1. 程式人生 > >目錄管理與文件

目錄管理與文件

解釋 除了 單純 join 文件的 路徑 onerror elpa 函數

1.探索整個目錄樹
kv:增刪改查
目錄樹==文件夾
文件目錄位置==一串字符串
文件的絕對目錄與相對目錄

幾個系統命令:
%cd 文件目錄位置
%pwd

目錄樹=文件夾

import os   # 針對系統的模塊,比如對目錄樹的操作
test_path = r‘C:\study\jupyter‘
os.listdir(test_path)  #listdir顯示目錄列表
[‘.ipynb_checkpoints‘,
 ‘21點.ipynb‘,
 ‘51beiwanglu-zuoye.ipynb‘,
 ‘51memo.1.py‘,
 ‘51備忘錄v0.24.ipynb‘,
 ‘color_me.py‘,
 ‘guess21.py‘,
 ‘python01-test.ipynb‘,
 ‘python02-basic-num-str.ipynb‘,
 ‘python02-basic-num-str2.ipynb‘,
 ‘True False  返回值.ipynb‘,
 ‘Untitled.ipynb‘,
 ‘Untitled1.ipynb‘,
 ‘Untitled10.ipynb‘,
 ‘Untitled2.ipynb‘,
 ‘Untitled3.ipynb‘,
 ‘Untitled4.ipynb‘,
 ‘Untitled5.ipynb‘,
 ‘Untitled6.ipynb‘,
 ‘Untitled7.ipynb‘,
 ‘Untitled8.ipynb‘,
 ‘Untitled9.ipynb‘,
 ‘zhaofangtao.ipynb‘,
 ‘zuoye1.ipynb‘,
 ‘__pycache__‘,
 ‘作用域.ipynb‘,
 ‘函數的來龍去脈.ipynb‘,
 ‘單位轉換器.ipynb‘,
 ‘控制程序的每一個角落.ipynb‘,
 ‘翻譯小程序.ipynb‘,
 ‘解釋函數.ipynb‘]
os.getcwd()  #獲取當前的工作目錄
‘C:\\study\\jupyter‘
os.chdir(‘C:\study\python‘)  # 更改目錄os.chdir()
os.getcwd()  # 顯示當前的工作目錄current work dir
‘C:\\study\\python‘
os.mkdir(‘coop‘)  # os.mkdir()創建新目錄
os.listdir()
[‘coop‘]

路徑就是一條字符串

左右斜線

os.path.join()

os.path.exits()

os.path.isdir()

os.path.join(‘c‘, ‘d‘)  #拼接路徑的方法
‘c\\d‘
os.path.exists(‘C:\study\python‘)
True
os.path.exists(r‘C:\study\python‘)  # 目錄路徑是否存在
True

相對路徑和絕對路徑

一個點:當前工作目錄,特殊名稱,不是真的文件夾

兩個點:父級目錄

os.path.abspath(‘.‘)

os.path.isabs()

os.path.relpath()

os.path.abspath(‘.‘)  # 當前路徑的絕對路徑
‘C:\\study\\python‘
os.path.isabs(‘C:\\study\\python‘)
True
os.path.relpath(‘C:\\study\\python‘, ‘c:\\study‘)
‘python‘
os.path.relpath(‘c:\\study‘, ‘C:\\study\\python‘)
‘..‘

命令管理

路徑各級名稱

path.split(os.path.sep)

os.path.basename()

os.path.dirname()

刪除文件操作:謹慎操作

os.unlink

shutil.rmtree(path)

移動/復制

copy

copytree

move

os.getcwd()
‘C:\\study\\python‘
path1 = r‘C:\\study\\python‘
path2 = r‘C:\study\python‘
os.path.sep
‘\\‘
path1.split(os.path.sep)
[‘C:‘, ‘‘, ‘study‘, ‘‘, ‘python‘]
path2.split(os.path.sep)
[‘C:‘, ‘study‘, ‘python‘]
os.path.split(path1)
(‘C:\\\\study‘, ‘python‘)
os.path.split(path2)  # 貪婪匹配
(‘C:\\study‘, ‘python‘)
os.path.splitext(path1)
(‘C:\\\\study\\\\python‘, ‘‘)
os.path.splitext(path2)
(‘C:\\study\\python‘, ‘‘)
os.getcwd()
‘C:\\study\\python‘
path3 = r‘C:\study\python\test1.xlsx‘
os.path.splitext(path3)  # 將文件名的後綴給隔離開了
(‘C:\\study\\python\\test1‘, ‘.xlsx‘)
os.path.split(path3)  # os.path.spit(path3)沒能將文件後綴給隔離
(‘C:\\study\\python‘, ‘test1.xlsx‘)
########################
os.path.basename(path1)  # print目錄最後的文件夾或者文件名
‘python‘
os.path.basename(path3)  # print目錄最後的文件夾或者文件名
‘test1.xlsx‘
os.path.dirname(path2)  # print目錄樹排除最後文件後的目錄,排除了目錄python
‘C:\\study‘
os.path.dirname(path3)  # 排除了文件夾 test1.xlsx
‘C:\\study\\python‘
############################
os.getcwd()
‘C:\\study\\python‘
os.listdir()
[‘coop‘, ‘test1.txt‘, ‘test1.xlsx‘]
os.unlink(‘test1.xlsx‘)   # 刪除文件
#######################################
import shutil   # shutil  針對目錄的模塊
shutil.rmtree(‘coop‘)   # 刪除目錄
os.listdir()
[‘test1.txt‘]
os.getcwd()
‘C:\\study\\python‘
shutil.copytree(‘../jupyter/test2/‘, ‘../python/test3‘)  # 復制並改名
‘../python/test3‘
shutil.copytree(‘../jupyter/test1/‘,‘test1‘)  # 單純的復制
‘test1‘
shutil.copy(‘./test1.txt‘, ‘./test2.txt‘)   # 針對文件
‘./test2.txt‘
os.listdir()
[‘test1‘, ‘test1.txt‘, ‘test2.txt‘, ‘test3‘]
shutil.copy(‘./test1/‘,‘./test4/‘)
---------------------------------------------------------------------------

PermissionError                           Traceback (most recent call last)

<ipython-input-72-3d91307f948a> in <module>()
----> 1 shutil.copy(‘./test1/‘,‘./test4/‘)

c:\users\coop\miniconda3\envs\coop\lib\shutil.py in copy(src, dst, follow_symlinks)
    239     if os.path.isdir(dst):
    240         dst = os.path.join(dst, os.path.basename(src))
--> 241     copyfile(src, dst, follow_symlinks=follow_symlinks)
    242     copymode(src, dst, follow_symlinks=follow_symlinks)
    243     return dst

c:\users\coop\miniconda3\envs\coop\lib\shutil.py in copyfile(src, dst, follow_symlinks)
    118         os.symlink(os.readlink(src), dst)
    119     else:
--> 120         with open(src, ‘rb‘) as fsrc:
    121             with open(dst, ‘wb‘) as fdst:
    122                 copyfileobj(fsrc, fdst)

PermissionError: [Errno 13] Permission denied: ‘./test1/‘
shutil.move(‘../jupyter/21點.ipynb‘, ‘./‘)  # move 移動
‘./21點.ipynb‘
os.listdir()
[‘21點.ipynb‘, ‘test1‘, ‘test1.txt‘, ‘test2.txt‘, ‘test3‘]

目錄管理與文件