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
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
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