You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _episodes/07-command_line.md
+75-7
Original file line number
Diff line number
Diff line change
@@ -417,18 +417,83 @@ if __name__ == "__main__":
417
417
{: .challenge}
418
418
419
419
> ## Extension 1
420
-
> Modify your script so that people can specify a wildcard character to read and write multiple files at once.
420
+
> Modify your script so that people can specify a wildcard character to read and write multiple files at once.
421
421
>
422
422
> You should be able to call the script using:
423
423
>
424
424
> ~~~
425
-
> $ python analyze_mdout.py data/*.mdout
425
+
> $ python analyze_mdout.py "data/*.mdout"
426
426
> ~~~
427
427
> {: .language-bash}
428
+
> **Note** - In the command above, we have used quotations around the filename. If you are on Linux or Mac, you can leave out the quotations. Your computer will expand the `data/*.mdout` into a list of all the files. This will not work on Windows. If you use the apprroach which works for Mac/Linux, you will need find a way for an argument in argparse to be a list for this. See [the documentation](https://docs.python.org/3/library/argparse.html#the-add-argument-method) for the `add_argument` function - you will find the answer there!
429
+
>> ## Solution - All operating systems
430
+
>> If you are working on Windows, this is the solution you will need. For this solution, you use quotation marks around the filename, and
431
+
>> include the wildcard character (`*`). Recall that we used this character along with the `glob` library in the multiple file parsing lesson.
432
+
>>
433
+
>> ~~~
434
+
>> import os
435
+
>> import argparse
436
+
>> import glob
437
+
>>
438
+
>> # Get filename from argparse
439
+
>>
440
+
>> parser = argparse.ArgumentParser("This script parses amber mdout file to extract the total energy.")
441
+
>>
442
+
>> parser.add_argument("path", help="The filepath of the file to be analyzed.")
> You will need find a way for an argument in argparse to be a list for this. See [the documentation](https://docs.python.org/3/library/argparse.html#the-add-argument-method) for the `add_argument` function - you will find the answer there!
430
-
>> ## Solution
431
-
>> For this solution, you would have to find the option `nargs` which would be added to `add_argument` which tells argparse that it may receive more than one value for the argument. T
487
+
>> ## Solution - Mac and Linux
488
+
>> This solution will work for Mac and Linux operating systems. For this solution, you would use the script without the quotes (below).
489
+
>> Note that the solution given above will work for any operating system.
490
+
>>
491
+
>> ~~~
492
+
>> $ python analyze_mdout.py data/*.mdout
493
+
>> ~~~
494
+
>> {: .language-bash}
495
+
>>
496
+
>> For this solution, you would have to find the option `nargs` which would be added to `add_argument` which tells argparse that it may receive more than one value for the argument. This works on Mac and Linux because these operating systems will automatically create a list of files when you use the wildcard character (`*`).
432
497
>>
433
498
>> ~~~
434
499
>> import os
@@ -442,6 +507,7 @@ if __name__ == "__main__":
442
507
>>
443
508
>> args = parser.parse_args()
444
509
>>
510
+
>> # This will already be a list, so we don't need to use glob.
0 commit comments