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

[Python basics] file management

編輯:Python

file management

  • One 、 Basic steps of file operation
  • Two 、OS modular
  • 3、 ... and 、json Module details

One 、 Basic steps of file operation

1、 Read write file
mode Three types commonly used in :
r: Only read
w: Can only write ( But the previous content will be cleared 0
a+: Reading and writing ( The file is appended with contents )

#1、 Open file 
f=open('E:\pythonProject\ file \hello word',mode='a')
# 2、 File operations 
f.write('java\n')
#3、 Close file 
f.close()

2、 File pointer move position

with open('E:\pythonProject\ file \hello','w+') as f:
f.write('hello\n') write file
f.seek(0,0) Move the pointer to the beginning
print(" Current pointer position ",f.tell())
f.seek(0,2) Move the pointer to the end
print(" Current pointer position ", f.tell())
print(f.read()) The output file

Two 、OS modular

1、os System

""" try: There may be an error code except: If there is an anomaly , What to do fianlly: Is there anything abnormal , Will be implemented """
1、 Name of the output system
# windows System use platform, If it is linux System use os modular 
import os
import platform
try:
uname = os.uname()
except Exception:
uname = platform.uname()
finally:
print(uname)
2、 Get environment variables
envs = os.environ
print(envs)
3、 Directory name and file name stitching
#os.path.dirname Get the directory name of a file 
BASE_DIR = os.path.dirname(__file__) #__file__ Current file 
setting_file = os.path.join(BASE_DIR,'dev.conf') # join Splicing , Concatenate directory name and file name 
print(setting_file)
with open(setting_file,'w') as f: # Create this file 

2、 System path

3、 ... and 、json Module details

A lightweight data exchange format that exchanges languages .

1、 take python The object is encoded as json character string

# json modular 
# 1、 take python The object is encoded as json character string 
import json
users = {
'name': 'westos', 'age': 18,' City ':" Xi'an "}
json_str = json.dumps(users)
print(json_str, type(json_str))
#2、 Stored in another file 
with open('E:/pythonProject/ file /json.json', 'w') as f:
json.dump(users, f,ensure_ascii=False,indent=4) # ensure_ascii Set up Chinese ,indent Set Chinese indent 
print(' Storage success ')
# result : In the created file, it is shown as follows 
{

"name": "westos",
"age": 18,
"����": "����"
}

2、 take json The string is decoded to python object
Will be created before json Object to python object

import json
with open('E:\pythonProject\ file \json.json') as f:
python_obj=json.load(f)
print(python_obj,type(python_obj))
# result 
{
'name': 'westos', 'age': 18, ' City ': ' Xi'an '} <class 'dict'>

3、 Convert information to execl form
First you need to install two modules , These two modules can be downloaded faster from China , The way is as follows

""" How to install pandas? > pip install pandas -i https://pypi.douban.com/simple How to install excel Operating modules ? > pip install openpyxl -i https://pypi.douban.com/simple """

import pandas
hosts = [
{
'host':'1.1.1.1', 'hostname':'test1', 'idc':'ali'},
{
'host':'1.1.1.2', 'hostname':'test2', 'idc':'ali'},
{
'host':'1.1.1.3', 'hostname':'test3', 'idc':'huawei'},
{
'host':'1.1.1.4', 'hostname':'test4', 'idc':'ali'}
]
# 1. Convert data type 
df = pandas.DataFrame(hosts)
print(df)
# 2. Store in excel In file 
df.to_excel('E:\pythonProject\ file \hosts.xlsx')
print('success')

You can see... On the specified path execl form

It reads as follows


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