File tree Expand file tree Collapse file tree 3 files changed +26
-11
lines changed Expand file tree Collapse file tree 3 files changed +26
-11
lines changed Original file line number Diff line number Diff line change
1
+ THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.
Original file line number Diff line number Diff line change @@ -21,9 +21,11 @@ def get_args():
21
21
help = 'A positional argument' )
22
22
23
23
parser .add_argument ('-o' ,
24
- '--on' ,
25
- help = 'A boolean flag' ,
26
- action = 'store_true' )
24
+ '--outfile' ,
25
+ help = 'Output filename' ,
26
+ metavar = 'str' ,
27
+ type = str ,
28
+ default = '' )
27
29
28
30
return parser .parse_args ()
29
31
@@ -33,21 +35,22 @@ def main():
33
35
"""Make a jazz noise here"""
34
36
35
37
args = get_args ()
36
- flag_arg = args .on
38
+ out_arg = args .outfile
37
39
pos_arg = args .positional
38
40
39
41
str = ''
40
42
41
- if flag_arg :
42
- if os .path .isfile (pos_arg ):
43
- str = open (pos_arg ).read ().rstrip ().upper ()
44
- else :
45
- #ERROR PATH
46
- print ('This is not working' )
43
+ if os .path .isfile (pos_arg ):
44
+ str = open (pos_arg ).read ().rstrip ().upper ()
47
45
else :
48
46
str = pos_arg .upper ()
49
47
50
- print (str )
48
+ if out_arg :
49
+ out_fh = open (out_arg ,'wt' )
50
+ print (str , file = out_fh )
51
+ out_fh .close ()
52
+ else :
53
+ print (str )
51
54
52
55
53
56
# --------------------------------------------------
Original file line number Diff line number Diff line change @@ -176,10 +176,21 @@ os.path.basename(file)
176
176
177
177
os.getcwd() # Current directory
178
178
179
+ # Reading
180
+
179
181
file_handler = open (file )
180
182
file_handler.read() # Empties the file handler
181
183
file_handler.seek(0 ) # To return to start and be able to read again
182
184
183
185
str = open (file ).read().rstrip() # Better (plus option to trim right)
184
186
187
+ # Writing
188
+
189
+ out_fh = open (' new.txt' ,' wt' ) # Modes : r/w/append, and text/bytes
190
+
191
+ out_fh.write(' Some text plus a return feed because else its missing \n ' )
192
+ print (' or via print and then no need for the return' , file = out_fh)
193
+
194
+ out_fh.close()
195
+
185
196
```
You can’t perform that action at this time.
0 commit comments