> 文章列表 > Python if 语句、Python3 os.rmdir() 方法

Python if 语句、Python3 os.rmdir() 方法

Python if 语句、Python3 os.rmdir() 方法

Python if 语句

以下实例通过使用 if...elif...else 语句判断数字正数、负数或零:

# -*- coding: UTF-8 -*-# Filename : test.py
# author by : www.w3cschool.cn# 用户输入数字num = float(input("输入一个数字: "))
if num > 0:print("正数")
elif num == 0:print("零")
else:print("负数")

执行以上代码输出结果为:

$ python test.py 
输入一个数字: 3
正数

我们也可以使用内嵌 if 语句来实现:

# -*- coding: UTF-8 -*-# Filename :test.py
# author by : www.w3cschool.cn# 内嵌 if 语句num = float(input("输入一个数字: "))
if num >= 0:if num == 0:print("零")else:print("正数")
else:print("负数")

执行以上代码输出结果为:

$ python test.py 
输入一个数字: 0
零

Python3 os.rmdir() 方法


概述

os.rmdir() 方法用于删除指定路径的目录。仅当这文件夹是空的才可以, 否则, 抛出OSError。

语法

rmdir()方法语法格式如下:

os.rmdir(path)

参数

  • path -- 要删除的目录路径

返回值

该方法没有返回值

实例

以下实例演示了 rmdir() 方法的使用:

#!/usr/bin/python3import os, sys# 列出目录
print ("目录为: %s"%os.listdir(os.getcwd()))# 删除路径
os.rmdir("mydir")# 列出重命名后的目录
print ("目录为: %s" %os.listdir(os.getcwd()))

执行以上程序输出结果为:

目录为:
[  'a1.txt','resume.doc','a3.py','mydir' ]
目录为:
[  'a1.txt','resume.doc','a3.py' ]