Python_1

作者: Latupa_天空之城 | 来源:发表于2019-10-13 21:36 被阅读0次
os 模块:

1.os.getcwd 函数 获取当前工作目录,python的工作目录

import os
pwd=os.getcwd()
print(pwd)
  1. os.name 函数 获取当前的操作系统,"nt"是windows,"posix"是Linux或者unix.
import os
name=os.name
if name=='posix':
     print("this is linux or unix")
elif name=='nt':
     print("this is windows")
else:
     print("this is other system")
  1. os.remove 函数 删除文件
import os
os.remove('A-B.xls')
  1. os.removedirs 函数 删除文件夹
import os
os.removedirs('file')

5.os.system 运行shell命令

import os
os.system('mv heatmap.go heatmap.go1')

6.os.mkdir() 创建文件夹

import os
os.mkdir('test')
  1. os.chdir() 改变当前工作路径到别的工作路径
import os
filepath='/media/wushsh/Result_Wushsh'
pwa=os.getcwd()
print(pwd)
os.chdir(filepath)
pwa=os.getcwd()
print(pwd)
  1. os.listdir() 列出当前目录下的所有文件夹和文件
import os
pwd=os.getcwd()
name=os.listdir(pwd)
for filename in name:
     print(filename)

supplement:

import os
print os.getcwd()   #输出当前的工作路径
print os.listdir('.')   #输出当前目录下所有的文件

相关文章

  • Python_1

    1.当使用Python2版本有中文的时候要添加# -- coding:utf-8 -- Python3版本中不需要...

  • Python_1

    1. #使用库 import math math.sqrt(100) 2. #类型转换 a = "3.14" fl...

  • Python_1

    os 模块: 1.os.getcwd 函数 获取当前工作目录,python的工作目录 os.name 函数 获取当...

网友评论

      本文标题:Python_1

      本文链接:https://www.haomeiwen.com/subject/pbikmctx.html