Python中os.path用法分析

yipeiwu_com5年前Python基础

本文实例分析了Python中os.path用法。分享给大家供大家参考。具体如下:

复制代码 代码如下:
#coding=utf-8
import os
print os.path.abspath("d:\\new\\test.txt")
print os.path.basename("d:\\new\\test.txt")
print os.path.dirname("d:\\new\\test.txt")
print os.path.exists("d:\\new")
print os.path.lexists("d:\\new")
print os.path.expanduser("d:\\new\\text.txt")
print os.path.getatime("d:\\new")  #最后访问时间
print os.path.getmtime("d:\\new") #最后修改路径时间
print os.path.getctime("d:\\new")  #创建时间
print os.path.getsize("d:\\new\\")  #或许路径的大小 字节为单位
print os.path.isabs("d:\\")  #是否是绝对路径
print os.path.isfile("d:\\new\\hello.txt")
print os.path.isdir("d:\\new")
print os.path.islink("d:\\new\\hello.txt")
print os.path.join("d:\\new","hello.txt")
print os.path.normcase("d:\\new\\hello.txt")
print os.path.relpath("d:\\new\\hello.txt")  #相对路径
print os.path.split("d:\\new\\hello.txt")  #分离文件名
print os.path.splitdrive("d:\\new\\hello.txt")  #分离磁盘驱动器
print os.path.splitext("d:\\new\\hello.txt")  #分离扩展名

  
运行结果:
>>>
d:\new\test.txt
test.txt
d:\new
True
True
d:\new\text.txt
1322235096.47
1322235096.47
1321610018.9
16384
True
True
True
False
d:\new\hello.txt
d:\new\hello.txt
hello.txt
('d:\\new', 'hello.txt')
('d:', '\\new\\hello.txt')
('d:\\new\\hello', '.txt')
>>>

希望本文所述对大家的Python程序设计有所帮助。

相关文章

Python实现的批量下载RFC文档

RFC文档有很多,有时候在没有联网的情况下也想翻阅,只能下载一份留存本地了。 看了看地址列表,大概是这个范围: http://www.networksorcery.com/enp/rfc...

基于python历史天气采集的分析

基于python历史天气采集的分析

分析历史天气的趋势。 先采集 代码: #-*- coding:utf-8 -*- import requests import random import MySQLdb im...

python使用datetime模块计算各种时间间隔的方法

本文实例讲述了python使用datetime模块计算各种时间间隔的方法。分享给大家供大家参考。具体分析如下: python中通过datetime模块可以很方便的计算两个时间的差,dat...

压缩包密码破解示例分享(类似典破解)

昨天翻硬盘,找到一个好东西,可惜自己加了密码自己不记得了。试了几个常用的没试出来,于是写了这么个小脚本来替我尝试。。呵呵,还真给解出来了。python脚本内容如下,跑跑自己加密的压缩包还...

用Python获取摄像头并实时控制人脸的实现示例

实现流程 从摄像头获取视频流,并转换为一帧一帧的图像,然后将图像信息传递给opencv这个工具库处理,返回灰度图像(就像你使用本地静态图片一样) 程序启动后,根据监听器信息,使用一个w...