File tree Expand file tree Collapse file tree 5 files changed +53
-0
lines changed Expand file tree Collapse file tree 5 files changed +53
-0
lines changed Original file line number Diff line number Diff line change
1
+ [DEFAULT]
2
+ UseTheForce = True
3
+ UseTheLightSaber = True
4
+ ForceReFill = 30
5
+ LightSaberPower = 20
6
+
7
+ [Section 1]
8
+ Title = " How to use the Force"
9
+
10
+ [Section 2]
11
+ Title = " How to use the light saber"
Original file line number Diff line number Diff line change
1
+ import configparser
2
+
3
+ parser = configparser .ConfigParser ()
4
+
5
+ parser .read ("config.cfg" )
6
+
7
+ print (parser .sections ())
8
+ print (parser .has_section ("Section 2" ))
9
+
10
+ UseTheLightSaber = bool (parser ['DEFAULT' ]['UseTheLightSaber' ])
11
+ print (UseTheLightSaber )
12
+ print (type (UseTheLightSaber ))
13
+
14
+ use_the_force = parser ['DEFAULT' ].getboolean ('UseTheForce' )
15
+ print (use_the_force )
16
+ # light_saber_power = parser['DEFAULT'].getfloat('LightSaberPower')
17
+ # print(light_saber_power)
18
+
19
+ light_saber_power = parser ['DEFAULT' ].getint ('LightSaberPower' )
20
+ print (light_saber_power )
21
+
22
+ try :
23
+ title = parser ['Section 3' ]['Title' ]
24
+ print (title )
25
+ except KeyError as err :
26
+ print ("No such thing as " , err )
Original file line number Diff line number Diff line change
1
+
2
+ fp = open ("test.txt" , "w" )
3
+ fp .write ("test test test\n " )
4
+ fp .close ()
5
+
6
+ with open ("test.txt" , "r" ) as fp :
7
+ data = fp .read ()
8
+ print (data )
9
+
10
+ with open ("test.txt" , "a+" ) as fp :
11
+ fp .write ("added more tests\n " )
12
+ fp .seek (0 )
13
+ data = fp .read ()
14
+ print (data )
Original file line number Diff line number Diff line change
1
+ test test test
2
+ added more tests
You can’t perform that action at this time.
0 commit comments