os.path.join(path,name) #连接目录与文件名或目录 结果为path/name

os.getcwd() get current working dir

os.chdir() change current dir

os.remove() delete file

os.rmdir() delete dir

os.mkdir() create dir

os.path.isfile() return bool,#如果path是一个文件,则返回True

os.path.isdir() return bool ,#如果path是一个目录,则返回True

os.system() excute the shell cmd

os.listdir() list all the filename and dir name of current dir in a list

os.path.getsize() get the size of file ,if is dir return 0,will return the size in bytes of the file in the_path_argument.

Notice how when I callos.path.getsize(), I useos.path.join()to join the folder name with the current filename.

 If I want to find the total size of all the files in this directory,
  I can use os.path.getsize() and os.listdir() together.


>>> totalSize = 0
>>> for filename in os.listdir('C:\\Windows\\System32'):
      totalSize = totalSize + os.path.getsize(os.path.join('C:\\Windows\\System32', filename))

>>> print(totalSize)
1117846456

os.path.abspath()

os.path.basename(path) return the fielname

>>> path = 'C:\\Windows\\System32\\calc.exe'
>>> os.path.basename(path)
'calc.exe'
>>> os.path.dirname(path)
'C:\\Windows\\System32'

os.path.split() 把目录和文件名分开,return a tuple

>>> import os
>>> os.path.split("a/b/c/d")
('a/b/c', 'd')
>>> os.path.split("a/b/c/d/")
('a/b/c/d', '')
import os
print(os.getcwd()) #返回当前路径,无参数
print(os.listdir('E:\zsfile')) #该路径下所有文件名
os.remove('E:\zsfile\q.py') #删除文件
os.mkdir('E:\za') #创建目录
os.rmdir('E:\za') #删除目录
os.chdir('E:\\rest') #切换目录
print(os.getpid()) #获取进程的ip
print(os.getppid()) #获取父进程的ip


print(os.path.basename(__file__)) #返回文件名
print(os.path.dirname(__file__)) #返回路径目录名
print(os.path.abspath(__file__)) #绝对路径
print(os.path.split(__file__))  #分开返回目录名和文件名,元组格式
print(os.path.exists(__file__)) #是否存在
print(os.path.isdir(__file__)) #是否为目录
print(os.path.isfile(__file__)) #是否为文件
print(os.path.getsize(__file__)) #返回文件大小
print(os.path.join('D:\zsfile','ZS.py')) #连接目录和文件

results matching ""

    No results matching ""