OS 模块:

OS模块:获取当前使用的操作系统

1、os.getcwd()函数

功能:获取当前目录,python 的工作目录

import os

pwd = os.getcwd()

print (pwd)

2、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")

3、os.remove()函数

功能:删除指定文件

eg:删除 file.txt 文件

import os

os.remove(’file.txt‘)

4、os.removedirs()函数

功能:删除指定目录

eg:删除 file目录

import os

os.removedirs(‘file’)

5、os.system()函数

功能:运行shell命令

eg:执行ls -a > 1.txt命令

import os

os.system(‘ls -a > 1.txt’)

6、os.mkdir()函数

功能:创建一个新目录

eg:创建一个 file 目录

import os

os.mkdir(‘file’)

7、os.chdir()函数

功能:改变当前路径到指定路径

eg:我现在从当前路径到 filepath 所指定的路径下

import os

filepath = '/home'

pwd = os.getcwd()

print (pwd)

os.chdir(filepath)

pwd = os.getcwd()

print (pwd)

8、os.listdir()函数

功能:返回指定目录下的所有目录和文件

eg:列出当前目录下的所有文件和目录

import os

pwd = os.getcwd()

name = os.listdir(pwd)

for filename in name:

 print (filename)

 

posted on 2022-03-07 11:21  属于我的梦,明明还在  阅读(347)  评论(0)    收藏  举报