os.path.normpath()

Python中的OS模块提供了与操作系统进行交互的功能。操作系统属于Python的标准实用程序模块。该模块提供了使用依赖于操作系统的功能的便携式方法。 os.path模块是Python中OS模块的子模块,用于通用路径名操作。

os.path.normpath()Python中的方法用于规范化指定的路径。在路径规范化过程中,所有冗余分隔符和up-level引用均折叠。
例如: A //B,A /B /,A /./B和A /foo /../B都将被标准化为A /B。在Windows操作系统上,路径中的任何正斜杠('/')都将转换为反斜杠('\')。

用法: os.path.normpath(path)

参数:
path:代表文件系统路径的path-like对象。

返回类型:此方法返回代表标准化路径的字符串值。

代码1:用于os.path.normpath()方法

# Python program to explain os.path.normpath() method  
    
# importing os.path module  
import os.path 
  
# Path 
path = '/home//user/Documnets'
  
  
# Normalize the specified path 
# using os.path.normpath() method 
norm_path = os.path.normpath(path) 
  
# Print the normalized path   
print(norm_path) 
  
# Path 
path = '/home/./Documents'
  
  
# Normalize the specified path 
# using os.path.normpath() method 
norm_path = os.path.normpath(path) 
  
# Print the normalized path   
print(norm_path) 
  
# Path 
path = '/home/user/temp/../Documents'
  
# Normalize the specified path 
# using os.path.normpath() method 
norm_path = os.path.normpath(path) 
  
# Print the normalized path   
print(norm_path)

输出:

/home/user/Documnets
/home/Documents
/home/user/Documents

代码2:用于os.path.normpath()方法(在Windows上)

# Python program to explain os.path.normpath() method  
    
# importing os.path module  
import os.path 
  
# Path 
path = r'C:/Users'
  
  
# Normalize the specified path 
# using os.path.normpath() method 
norm_path = os.path.normpath(path) 
  
# Print the normalized path   
print(norm_path) 
  
# Path 
path = r'C:\Users\.\Documents'
  
  
# Normalize the specified path 
# using os.path.normpath() method 
norm_path = os.path.normpath(path) 
  
# Print the normalized path   
print(norm_path) 
  
# Path 
path = r'C:\Users\admin\temp\..\Documents'
  
# Normalize the specified path 
# using os.path.normpath() method 
norm_path = os.path.normpath(path) 
  
# Print the normalized path   
print(norm_path)

输出:

C:\\Users
C:\\Users\\Documents
C:\\Users\\admin\\Documents

  • 15
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wanderer001

ROIAlign原理

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值