关于python文件操作的详细说明,大家可以参考:关于python的文件操作
官方API:os-Miscellaneous operating system interfaces
下面是我做的demo:
1 import re 2 import os 3 import time 4 5 #图片文件路径 6 image_path = \'E:\\test\\20130627_140132Hongten.jpg\' 7 8 #文件夹路径 9 dir_path = \'E:\\test\\hongten\' 10 11 #文件路径 12 file_abs_path = \'E:\\test\\hongten.txt\' 13 14 #得到当前工作空间目录 15 def getcwd(): 16 return os.getcwd() 17 18 #获取指定文件夹下面的所有文件及文件夹 19 #如果指定的文件夹不存在,则返回相应的提示信息 20 def listdir(dir_path): 21 if os.path.exists(dir_path): 22 return os.listdir(dir_path) 23 else: 24 return \'目录\'+ dir_path + \'不存在\' 25 26 def isfile(file_path): 27 if os.path.exists(file_path): 28 return os.path.isfile(file_path) 29 else: 30 return \'文件\'+ dir_path + \'不存在\' 31 32 33 if __name__ == \'__main__\': 34 print(\'当前的工作空间是:{0}\'.format(getcwd())) 35 print(\'当前的工作空间下的文件及目录:\',listdir(getcwd())) 36 print(\'#\' * 40) 37 print(listdir(\'c:\\test\')) 38 print(\'#\' * 40) 39 print(isfile(image_path)) 40 print(\'#\' * 40) 41 array = os.path.split(image_path) 42 print(array) 43 #文件全名:20130627_140132Hongten.jpg 44 file_full_name = array[1] 45 name = os.path.splitext(file_full_name) 46 #文件名:20130627_140132Hongten 47 file_name = name[0] 48 #文件后缀:.jpg 49 file_ext = name[1] 50 print(\'文件全名:{0},文件名:{1},文件后缀:{2}\'.format(file_full_name,file_name,file_ext)) 51 print(\'#\' * 40) 52 #创建空文件夹 53 #os.mkdir(\'E:\\mydir\') 54 #创建多级目录 55 #os.makedirs(r\'E:\\bb\\cc\') 56 print(\'#\' * 40) 57 #打开一个文件 58 fp = open(file_abs_path,\'w+\') 59 #print(\'读取文件:{0}的第一行:{1}\'.format(file_abs_path,fp.readline())) 60 #把文件每一行作为一个list的一个成员,并返回这个list。其实它的内部是通过循环调用readline()来实现的。 61 #如果提供size参数,size是表示读取内容的总长,也就是说可能只读到文件的一部分。 62 #print(\'读取文件:{0}所有内容:{1}\'.format(file_abs_path,fp.readlines())) 63 content = \'this is a test message!!\ngood boy!\ngogo......\nhello,I\\'m Hongten\nwelcome to my space!\' 64 fp.write(content) 65 fp.flush() 66 fp.close() 67 fp = open(file_abs_path,\'r+\') 68 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!\'] >>>