目录管理与文件

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']

猜你喜欢

转载自blog.51cto.com/13118411/2136821
今日推荐