国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python開發之文件操作用法實例

2020-01-04 17:57:08
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了python開發之文件操作用法,以實例形式較為詳細的分析了Python針對文件的路徑、文件名、后綴名等操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了python開發之文件操作用法。分享給大家供大家參考,具體如下:

先來看看官方API:os-Miscellaneous operating system interfaces

下面是我做的demo:

 

 
  1. import re 
  2. import os 
  3. import time 
  4. #圖片文件路徑 
  5. image_path = 'E://test//20130627_140132Hongten.jpg' 
  6. #文件夾路徑 
  7. dir_path = 'E://test//hongten' 
  8. #文件路徑 
  9. file_abs_path = 'E://test//hongten.txt' 
  10. #得到當前工作空間目錄 
  11. def getcwd(): 
  12. return os.getcwd() 
  13. #獲取指定文件夾下面的所有文件及文件夾 
  14. #如果指定的文件夾不存在,則返回相應的提示信息 
  15. def listdir(dir_path): 
  16. if os.path.exists(dir_path): 
  17. return os.listdir(dir_path) 
  18. else
  19. return '目錄'+ dir_path + '不存在' 
  20. def isfile(file_path): 
  21. if os.path.exists(file_path): 
  22. return os.path.isfile(file_path) 
  23. else
  24. return '文件'+ dir_path + '不存在' 
  25. if __name__ == '__main__'
  26. print('當前的工作空間是:{0}'.format(getcwd())) 
  27. print('當前的工作空間下的文件及目錄:',listdir(getcwd())) 
  28. print('#' * 40) 
  29. print(listdir('c://test')) 
  30. print('#' * 40) 
  31. print(isfile(image_path)) 
  32. print('#' * 40) 
  33. array = os.path.split(image_path) 
  34. print(array) 
  35. #文件全名:20130627_140132Hongten.jpg 
  36. file_full_name = array[1] 
  37. name = os.path.splitext(file_full_name) 
  38. #文件名:20130627_140132Hongten 
  39. file_name = name[0] 
  40. #文件后綴:.jpg 
  41. file_ext = name[1] 
  42. print('文件全名:{0},文件名:{1},文件后綴:{2}'.format(file_full_name,file_name,file_ext)) 
  43. print('#' * 40) 
  44. #創建空文件夾 
  45. #os.mkdir('E://mydir') 
  46. #創建多級目錄 
  47. #os.makedirs(r'E://bb//cc') 
  48. print('#' * 40) 
  49. #打開一個文件 
  50. fp = open(file_abs_path,'w+'
  51. #print('讀取文件:{0}的第一行:{1}'.format(file_abs_path,fp.readline())) 
  52. #把文件每一行作為一個list的一個成員,并返回這個list。其實它的內部是通過循環調用readline()來實現的。 
  53. #如果提供size參數,size是表示讀取內容的總長,也就是說可能只讀到文件的一部分。 
  54. #print('讀取文件:{0}所有內容:{1}'.format(file_abs_path,fp.readlines())) 
  55. content = 'this is a test message!!/ngood boy!/ngogo....../nhello,I/'m Hongten/nwelcome to my space!' 
  56. fp.write(content) 
  57. fp.flush() 
  58. fp.close() 
  59. fp = open(file_abs_path,'r+'
  60. print('讀取文件:{0}所有內容:{1}'.format(file_abs_path,fp.readlines())) 

運行效果:

 

 
  1. Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32 
  2. Type "copyright""credits" or "license()" for more information. 
  3. >>> ================================ RESTART ================================ 
  4. >>>  
  5. 當前的工作空間是:D:/Python33/workspace 
  6. 當前的工作空間下的文件及目錄: ['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'
  7. ######################################## 
  8. 目錄c:/test不存在 
  9. ######################################## 
  10. True 
  11. ######################################## 
  12. ('E://test''20130627_140132Hongten.jpg'
  13. 文件全名:20130627_140132Hongten.jpg,文件名:20130627_140132Hongten,文件后綴:.jpg 
  14. ######################################## 
  15. ######################################## 
  16. 讀取文件:E:/test/hongten.txt所有內容:['this is a test message!!/n''good boy!/n''gogo....../n'"hello,I'm Hongten/n", 'welcome to my space!'] 
  17. >>> 

希望本文所述對大家Python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宁安市| 新乐市| 桦南县| 和平县| 班戈县| 万源市| 大理市| 通城县| 黎川县| 揭西县| 榆社县| 普格县| 朝阳区| 东丰县| 罗平县| 宝鸡市| 彰化县| 游戏| 太康县| 长垣县| 鹤庆县| 九江县| 法库县| 镇坪县| 德格县| 霍山县| 杂多县| 武邑县| 江山市| 沧州市| 宝丰县| 通渭县| 昌图县| 红桥区| 满城县| 西和县| 南投县| 榆社县| 盘锦市| 扎赉特旗| 渭南市|