程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[python learning 13] basic file and directory operations

編輯:Python

 (一)編碼格式的介紹

 

修改py文件的編碼格式:In the first line to write program #encoding=gbk 修改為gbk編碼格式 

(二)文件的讀寫原理(讀取磁盤文件中的內容)

 

file=open("a.txt","r") #"r"表示read
print(file.readlines()) #readlines()The contents of the text file will be read and in the form of a list to show
file.close()

 (三)常用的文件打開方式

source_file=open("logo.png",rb) #Source files in read mode of binary read
target_file=open("copy.png",wb) #The target file to write the binary file,目標文件是copy.png
target_file.write(source_file.read())
source_file.close()
target_file.close()

 (四)文件對象的常用方法

file=open('a.txt','r')
print(file.read())
print(file.readline())
print(file.readlines())#作為列表返回

tell()Method Pointers position from0開始算

flush()Methods after can still to write content file(Because there is no close the file),但close()Methods after not

(五)with語句(上下文管理器)

①If a class implements the special method__enter__()和__exit__(),The class has complied with the context management agreement,An instance of this class object is called context manager 

②離開運行時上下文,Automatically invocation context manager's special methods__exit__()進行退出

with open("c.txt","r") as file: #相當於file=open("c.txt","r")
print(file.read()) #不用再寫file.close(),因為離開withBlock automatically shut off when the file resources
#with語句實現文件的復制
with open("a.png","rb") as source_file:
with open("b.png","wb") as target_file:
target_file.write(source_file.read())

 (六)目錄操作

import os
os.system("notepad.exe") #打開記事本,也可在WIN中輸入notepad
os.system("calc.exe") #打開計算器,也可在WIN中輸入
#osModules can also be called directly executable file

 1,os模塊操作目錄相關函數

 

The part and functionubuntuThe system of the terminal command similar again 

This part of the generally used in辦公自動化

import os
print(os.getcwd())
lst=os.listdir("../chap15") #listdir()函數返回列表,".."表示上一級目錄
print(lst)
os.mkdir(newdir) #創建目錄
os.makedirs("/a/b/c") #創建多級目錄
os.rmdir(newdir) #刪除目錄
os.removedirs("a/b/c") #刪除多級目錄

2,os.path模塊操作目錄相關函數

import os.path
print(os.path.abspath('demo3.py'))#獲取demo3.py文件的絕對路徑
print(os.path.exists('demo13.py'),os.path.exists('12.txt'))#結果返回True/False
print(os.path.join('E:\\python','demo6.py')) #Before and after for Mosaic isE:\python\demo6.py
print(os.path.split('E:\\vippython\\chapter11\\demo.py'))#Separate file path and file name is('E:\\vippython\\chapter11','demo.py')
print(os.path.splitext('demo4.py'))#輸出{'demo4','.py'}
print(os.path.basename('E:\\vippython\\chapter11\\demo.py'))#輸出demo.py,從路徑中提取文件名
print(os.path.dirname('E:\\vippython\\chapter11\\demo.py'))#輸出E:\vippython\chapter11,Extracted from path directory,不包括文件名
print(os.path.isdir('E:\\vippython\\chapter11\\demo.py'))#輸出False,因為demo.py不是路徑名

(七) The actual case code

辦公自動化 Just started to learn is 文件處理 

#列出指定目錄下的所有.py文件
import os
path=os.getcwd() #獲取當前路徑,Of course you can also specify the corresponding path
lst=os.listdir(path)
for filename in lst:
if filename.endswith('.py'):
print(filename)
#Successive home directory and subdirectory corresponding directory path,目錄名,文件名
import os
path=os.getcwd()#獲取當前路徑,也可以指定路徑
lst_files=os.walk(path)
for dirpath,dirname,filename in lst_files: #Will step by step a successive visit home directory and subdirectory paths,目錄名,文件名
print(dirpath) #Output current directory path
print(dirname) #The output of the current directory subdirectory name
print(filename) #The output file name is not a subdirectory of the current directory

 第二個代碼塊中,用到了 osAn important method in modulewalk(), 該方法可以Recursive traversal of the home directory and neutron directory home directory file

(八)知識點總結


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved