1. window下的路径分隔符和mac上的一样吗
不一样。
Windows | Mac | |
---|---|---|
根文件夹 | C:\ | / |
附加卷 | D:\或E:\ | /mnt |
路径分隔符 | \ | / , 或者import os , 然后\\ |
2. 如何查看当前工作目录
>>> import os
>>> os.getcwd()
'C:\\Users\\DELL\\AppData\\Local\\Programs\\Python\\Python37'
3. 如何切换当前工作目录
>>> os.chdir('c:\\Program Files (x86)\\2345Soft')
>>> os.getcwd()
'c:\\Program Files (x86)\\2345Soft'
4. 如何创建新文件夹
>>> os.makedirs('C:\\Users\\DELL\\Desktop\\survey')
5. 如何将绝对路径修改为相对路径
-
os.path.abspath(path)将相对路径转换为绝对路径;
>>> os.path.abspath('.') 'C:\\Users\\DELL\\AppData\\Local\\Programs\\Python\\Python37' >>> os.path.abspath('.\\Scripts') 'C:\\Users\\DELL\\AppData\\Local\\Programs\\Python\\Python37\\Scripts'
-
os.path.relpath(path,start)将绝对路径转换为相对路径;
>>> os.path.relpath('C:\\Windows','C:\\Dell') '..\\Windows'
6. 如何查看一个路径的目录名称、基本名称。
-
查看目录名称:os.path.dirname(path)
>>> os.getcwd() 'C:\\Users\\DELL\\AppData\\Local\\Programs\\Python\\Python37' >>> path='C:\\Users\\DELL\\AppData\\Local\\Programs\\Python\\Python37' >>> os.path.dirname(path) 'C:\\Users\\DELL\\AppData\\Local\\Programs\\Python'
-
查看基本名称:os.path.basename(path)
>>> os.path.basename(path) 'Python37'
7. os.listdir(path) 查看文件夹里面的内容
>>> os.listdir('C:\\Users\\DELL\\AppData\\Local\\Programs\\Python\\Python37')
['DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'NEWS.txt', 'python.exe', 'python3.dll', 'python37.dll', 'pythonw.exe', 'Scripts', 'tcl', 'Tools', 'vcruntime140.dll']
8. os.path.getsize(path) 查看一个文件大小
-
一个程序的字节数
>>> os.path.getsize('C:\\Users\\DELL\\AppData\\Local\\Programs\\Python\\Python37\\python.exe') 99856
-
一个目录下所有文件的字节数
>>> totalsize=0 >>&g