import json #use json file ,you must import json.
def verify_file_class():
file_json=open(r'C:\temp\config_file.json','r') # open config_file.json file with 'r'
for each_line in file_json.readlines(): #read each line data
print each_line # verify each line data by print each line data
each_line_dict = json.loads(each_line) # each row of the data into the 'dict'type of python
print 'the type of the each_line_dict:{type}'.format(type=type(each_line_dict)) # verify whether is‘dict'type
print 'user is: {user}'.format(user=each_line_dict['user'])
print 'username is: {username}'.format(username=each_line_dict['username'])
print 'password is: {password}'.format(password=each_line_dict['password'])
print 'ipaddr is: {ipaddr} \n'.format(ipaddr=each_line_dict['ipaddr'])
#use username,password, ipaddr ( enjoy your programming ! )
file_json.close() # don't forgot to close your open file before.
if __name__ == '__main__':
verify_file_class()
{"user":"Tom","username":"root_tom","password":"Jerryispig","ipaddr":"10.168.79.172"}
the type of the each_line_dict:<type 'dict'>
user is: Tom
username is: root_tom
password is: Jerryispig
ipaddr is: 10.168.79.172
{"user":"Jerry","username":"root_jerry","password":"Tomispig","ipaddr":"10.168.79.173"}
the type of the each_line_dict:<type 'dict'>
user is: Jerry
username is: root_jerry
password is: Tomispig
ipaddr is: 10.168.79.173
import os
import shutil
def empty_folder(dir):
try:
for each in os.listdir(dir):
path = os.path.join(dir,each)
if os.path.isfile(path):
os.remove(path)
elif os.path.isdir(path):
shutil.rmtree(path)
return 0
except Exception as e:
return 1
if __name__ == '__main__':
dir_path=r'D:\installation'
empty_folder(dir_path)
1. os.listdir(dir) 2. os.path.join(dir, each) 3. os.path.isfile(path) /os.path.isdir(path) 4. os.remove(path) 5. shutil.rmtree(path)
>>> import os >>> os.listdir(r'c:\\') ['$Recycle.Bin', 'Documents and Settings', 'eclipse', 'hiberfil.sys', 'inetpub', 'Intel', 'logon_log.txt', 'MSOCache', 'pagefile.sys', 'PerfLogs'<span style="font-family: Arial, Helvetica, sans-serif;">]</span>
>>> import os >>> os.path.join(r'c:\doog',r's.txt') 'c:\\doog\\s.txt' >>> os.path.join(r'c:\doog',r'file') 'c:\\doog\\file'
>>> import os >>> filepath=r'C:\Program Files (x86)\Google\Chrome\Application\VisualElementsManifest.xml' >>> os.path.isdir(filepath) False >>> os.path.isfile(filepath) True
>>> import os >>> os.removedirs(r'c:\temp\david\book\python.txt')
>>> import shutil >>> shutil.rmtree(r'C:\no1')
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有