国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

腳本之家,腳本語言編程技術(shù)及教程分享平臺!
分類導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Python - python開發(fā)之文件操作用法實(shí)例

python開發(fā)之文件操作用法實(shí)例

2020-08-02 10:59Hongten Python

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

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

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

下面是我做的demo:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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'
#得到當(dāng)前工作空間目錄
def getcwd():
  return os.getcwd()
#獲取指定文件夾下面的所有文件及文件夾
#如果指定的文件夾不存在,則返回相應(yīng)的提示信息
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('當(dāng)前的工作空間是:{0}'.format(getcwd()))
  print('當(dāng)前的工作空間下的文件及目錄:',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)
  #創(chuàng)建空文件夾
  #os.mkdir('E:\\mydir')
  #創(chuàng)建多級目錄
  #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。其實(shí)它的內(nèi)部是通過循環(huán)調(diào)用readline()來實(shí)現(xiàn)的。
  #如果提供size參數(shù),size是表示讀取內(nèi)容的總長,也就是說可能只讀到文件的一部分。
  #print('讀取文件:{0}所有內(nèi)容:{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}所有內(nèi)容:{1}'.format(file_abs_path,fp.readlines()))

運(yùn)行效果:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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āng)前的工作空間是:D:\Python33\workspace
當(dāng)前的工作空間下的文件及目錄: ['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所有內(nèi)容:['this is a test message!!\n', 'good boy!\n', 'gogo......\n', "hello,I'm Hongten\n", 'welcome to my space!']
>>>

希望本文所述對大家Python程序設(shè)計(jì)有所幫助。

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 久久国产精品一区二区 | 久久亚 | 国产一区二区三区免费在线观看 | 成人免费一区二区三区视频网站 | 一区二区日韩 | 中文字幕av黄色 | 午夜窝窝 | 一区二区亚洲 | 久久99精品久久久久久噜噜 | 91精品国产一区二区三区香蕉 | 久久久国产精品免费观看 | 久草电影网 | 夜夜爽99久久国产综合精品女不卡 | 久久精品国产亚洲 | 综合久久av| 成人午夜电影网 | 成人av免费 | 极品美女销魂一区二区三区 | 黄色免费高清网站 | 国产成人午夜 | 日本一区二区视频在线播放 | 成人精品在线观看 | 日韩精品一区在线 | 三级国产网站 | 精品一区二区三区中文字幕老牛 | 最近免费中文字幕在线视频2 | 91精品国产乱码久久久久久 | 成人免费毛片aaaaaa片 | 精品视频免费观看 | 国产精品久久久久久久久久新婚 | 毛片免费在线视频 | 日本一区二区三区日本免费 | 热久久国产 | 成人男女啪啪免费观软件 | 影音先锋资源av | 精品一区视频 | 在线一级毛片 | 欧美一区二区三区精品 | 日本人在线观看 | 国产一区二区三区精品久久久 | 久久久在线 |