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

Advanced Python Programming -- file processing and OS module

編輯:Python

Catalog

  • 1. Basic operation of documents
  • 2. Write data
  • 3. Reading data
  • 4. Append write
  • 5. File path
  • 6. Access pattern r w a r+ w+ a+
  • 7. Read operation
  • 8. How to read files
  • 9. File and folder operations


1. Basic operation of documents

# Document processing 
# file : Audio , video , Photo ,html
# Basic operation of documents Open file Read file data Write file data Close file data 
# effect : To facilitate data management Store the data You can use it directly next time 
# open open( File path ,)
# Read read
# write in write
# close close

2. Write data

# open test1.txt file , Write data w
f = open('test1.txt','w',encoding = 'utf-8')
f.write(' Hello, hello ')
# print(f.read())
f.close()

3. Reading data

f = open('test1.txt','r',encoding = 'utf-8')
print(f.read())
f.close()

4. Append write

# w Yes write , No files create files w It will cover the original content 
# r Is read , No file will not be created 
# a It's an addition 
f = open('test1.txt','a',encoding = 'utf-8')
f.write(' You're so funny ')
f.close()
f = open('test1.txt','w',encoding = 'utf-8')
f.write(' Are you kidding ?')
f.close()

5. File path

# open( File path , Access pattern )
# Absolute path : Detailed address 
# Relative paths : Run the file Relative to your path 
# ./ Current directory Run the file in the root directory You are looking for the contents of the folder find class06 In the room test1.txt How to find Relative paths 
# f = open('./class06/test1.txt','r')
# print(f.read())
f.close()
# ../ Superior directory The running file is in the folder I'm looking for class05 In a folder test1.txt
f=open('../class06/test1.txt','r',encoding='utf-8')
print(f.read())
f.close()
# Be careful : Where are your running files 
# Absolute path : Detailed address 
f = open(r'C:\Users\user\PycharmProjects\cema\class06\test1.txt','r',encoding='utf-8')
print(f.read())
f.close()
# File path open( File path , Access pattern 
# Relative paths Absolute path 

6. Access pattern r w a r+ w+ a+

# r+ Read can write 
f = open('test1.txt','r+',encoding='utf-8')
print(f.read())
f.close()
f = open('test1.txt','r+',encoding='utf-8')
print(f.tell())
# tell Pointer position 
# Put the pointer to the end , Offset pointer position seek seek( Number of bytes , Pointer position )0 start ,1 The current position 2 Ending position 
# f.seek(0,2)
f.seek(0,1)
print(f.tell())
f.write(' Si Xiaoyou ')
f.close()
# w+ It will cover up 
f = open('test1.txt','w+',encoding='utf-8')
print(f.tell())
f.seek(0,2)
f.write(' Hello , Mamma Mia ')
f.close()
# a+
f = open('test1.txt','a+',encoding='utf-8')
print(f.tell())
f.seek(0,0)
f.write('\n I'm here to add ')
f.close()
# rb wb ab Binary 
# Binary : Out-of-service txt open 
# text file : It works txt open 

7. Read operation

# read() Read entire file 
# readline(): Read a line 
# readlines(): Read all of them and put them in the list 
# Read the contents of a line 
f=open('test1.txt','r',encoding='utf-8')
# print(f.read())
# print(f.readline())
# print(f.readlines())
print(f.readlines()[1])
f.close()

8. How to read files

# with open( route , Access pattern ) as Variable name :
# Code block 
with open('test1.txt','r',encoding='utf-8') as f:
print(f.read())
# This method does not need to be closed 
# File path and operation of reading file Read yaml file 

9. File and folder operations

# os modular : Provides operations for processing files and file directories 
import os
# Create a folder w File path 
file=r'C:\Users\user\PycharmProjects\cema\class06\sixiaoyou'
# os.mkdir((file))
# Delete folder 
# os.rmdir(file)
# Non empty folder 
# import shutil
# shutil.rmtree(file)
# File rename 
# os.rename('test1.txt','test55.txt')
# Judgment is a document 
# print(os.path.isdir(file))
# Judgment is a document 
print(os.path.isfile(file))
# Get file path 
# Get the absolute path of the folder 
print(os.getcwd())
# The absolute path to the file 
# print(os.path.abspath(__file__))
print(__file__)
# The parent path of the current path 
# print(os.path.dirname(os.path.abspath(__file__)))
# database Absolute path config/conf.ini
print(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# C:\Users\user\PycharmProjects\cema\class06\config
a = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),'config')
print(a)
print(a+'\conf.ini')
print(a+'\info.yaml')

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