python开发之文件操作用法实例
作者:Hongten 时间:2022-08-07 02:03:45
本文实例讲述了python开发之文件操作用法。分享给大家供大家参考,具体如下:
先来看看官方API:os-Miscellaneous operating system interfaces
下面是我做的demo:
import re
import os
import time
#图片文件路径
image_path = 'E:\\test\\20130627_140132Hongten.jpg'
#文件夹路径
dir_path = 'E:\\test\\hongten'
#文件路径
file_abs_path = 'E:\\test\\hongten.txt'
#得到当前工作空间目录
def getcwd():
return os.getcwd()
#获取指定文件夹下面的所有文件及文件夹
#如果指定的文件夹不存在,则返回相应的提示信息
def listdir(dir_path):
if os.path.exists(dir_path):
return os.listdir(dir_path)
else:
return '目录'+ dir_path + '不存在'
def isfile(file_path):
if os.path.exists(file_path):
return os.path.isfile(file_path)
else:
return '文件'+ dir_path + '不存在'
if __name__ == '__main__':
print('当前的工作空间是:{0}'.format(getcwd()))
print('当前的工作空间下的文件及目录:',listdir(getcwd()))
print('#' * 40)
print(listdir('c:\\test'))
print('#' * 40)
print(isfile(image_path))
print('#' * 40)
array = os.path.split(image_path)
print(array)
#文件全名:20130627_140132Hongten.jpg
file_full_name = array[1]
name = os.path.splitext(file_full_name)
#文件名:20130627_140132Hongten
file_name = name[0]
#文件后缀:.jpg
file_ext = name[1]
print('文件全名:{0},文件名:{1},文件后缀:{2}'.format(file_full_name,file_name,file_ext))
print('#' * 40)
#创建空文件夹
#os.mkdir('E:\\mydir')
#创建多级目录
#os.makedirs(r'E:\\bb\\cc')
print('#' * 40)
#打开一个文件
fp = open(file_abs_path,'w+')
#print('读取文件:{0}的第一行:{1}'.format(file_abs_path,fp.readline()))
#把文件每一行作为一个list的一个成员,并返回这个list。其实它的内部是通过循环调用readline()来实现的。
#如果提供size参数,size是表示读取内容的总长,也就是说可能只读到文件的一部分。
#print('读取文件:{0}所有内容:{1}'.format(file_abs_path,fp.readlines()))
content = 'this is a test message!!\ngood boy!\ngogo......\nhello,I\'m Hongten\nwelcome to my space!'
fp.write(content)
fp.flush()
fp.close()
fp = open(file_abs_path,'r+')
print('读取文件:{0}所有内容:{1}'.format(file_abs_path,fp.readlines()))
运行效果:
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
当前的工作空间是:D:\Python33\workspace
当前的工作空间下的文件及目录: ['rename.py', 'test_annotation.py', 'test_class.py', 'test_exception.py', 'test_exit.py', 'test_file.py', 'test_getA.py', 'test_hello.py', 'test_import.py', 'test_input.py', 'test_loops.py', 'test_myclass.py', 'test_os.py', 'test_range.py', 'test_str.py', 'test_string.py', 'test_while.py', 'test_with.py']
########################################
目录c:\test不存在
########################################
True
########################################
('E:\\test', '20130627_140132Hongten.jpg')
文件全名:20130627_140132Hongten.jpg,文件名:20130627_140132Hongten,文件后缀:.jpg
########################################
########################################
读取文件:E:\test\hongten.txt所有内容:['this is a test message!!\n', 'good boy!\n', 'gogo......\n', "hello,I'm Hongten\n", 'welcome to my space!']
>>>
希望本文所述对大家Python程序设计有所帮助。
标签:python,文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python内建类型bytes深入理解
2022-11-13 08:35:54
![](https://img.aspxhome.com/file/2023/5/62935_0s.png)
php写入mysql中文乱码的实例解决方法
2023-10-10 01:36:49
asp 动态生成rss(不成生xml文件)代码
2011-04-04 11:17:00
PHP生成静态页面详解
2023-11-21 06:50:43
Python Prim算法通过遍历墙实现迷宫的生成
2022-06-26 08:41:09
Python中的字典遍历备忘
2021-12-08 05:59:37
Python批量修改xml的坐标值全部转为整数的实例代码
2021-10-29 00:57:50
![](https://img.aspxhome.com/file/2023/8/65548_0s.jpg)
详解python Todo清单实战
2021-11-03 06:24:50
![](https://img.aspxhome.com/file/2023/1/66771_0s.png)
js随机永不重复数
2011-04-25 19:26:00
Python中typing模块的具体使用
2022-03-25 13:02:08
asp如何在线更改密码?
2010-06-26 12:22:00
Pandas库之DataFrame使用的学习笔记
2023-07-03 23:34:28
![](https://img.aspxhome.com/file/2023/1/97371_0s.png)
Python合并多个装饰器小技巧
2022-05-31 04:51:45
Python数据处理numpy.median的实例讲解
2022-07-24 06:36:04
ASPJPEG组件简要攻略之水印、缩略图和描边代码
2008-12-17 12:08:00
tensorflow 自定义损失函数示例代码
2023-03-13 21:37:18
收集的几个Python小技巧分享
2023-06-14 01:54:01
关于Python 列表的索引取值问题
2022-09-08 05:39:54
基于python3.7利用Motor来异步读写Mongodb提高效率(推荐)
2022-12-20 08:44:05
![](https://img.aspxhome.com/file/2023/4/80474_0s.png)
Python Matplotlib绘制箱线图boxplot()函数详解
2021-03-09 11:16:07
![](https://img.aspxhome.com/file/2023/2/97222_0s.png)