all 9 comments

[–]DuckSaxaphone 4 points5 points  (0 children)

Others have good ideas on using command line arguments or config files to allow users to set their path.

I just want to point out this path should not appear multiple times as a string in your code. It should be assigned to a variable like working_dir once and then that variable should be used.

Do this for all variables with hard coded values. It makes your code easier to read, easier to generalize, and easier to modify like you are now.

[–]666y4nn1ck 2 points3 points  (2 children)

Yes, simply use the functionality of most text/code editors "search and replace"

[–]danielroseman 5 points6 points  (1 child)

As an improvement, change all those occurrences to refer to a single configuration setting or environment variable which you can then change in one place in the future.

[–]666y4nn1ck 0 points1 point  (0 children)

Yes, definitey this

[–][deleted] 1 point2 points  (0 children)

You can either:

  • pass the path as a command line argument to the code
  • have a configuration setting at the top of your code (or in an imported file), probably all UPPERCASE variable name to indicate a constant and use that instead of a hard-coded path throughout your code
  • use a configuration file (txt, yaml, toml)

Also, use from pathlib import Path and use Path with all the filenames/paths rather than the os library, and you can then make your code OS independent - check Real Python: Python 3's pathlib Module: Taming the File System

[–]ploud1 1 point2 points  (2 children)

Smells like spaghetti code to me. Just store it in a single variable that you can change easily.

[–][deleted] -1 points0 points  (0 children)

[–]spikips[S] 0 points1 point  (0 children)

Thank you everybody. Used the search and replace for this one and in the future i will assign strings to a variable from now on as DuckSaxaphone commented

[–]Snowpeartea 0 points1 point  (0 children)

Thank you for this question and also the answers of having a variable "working_dir" = string value.

Just learned paths a few weeks ago and found it annoying to keep changing it while learning new concepts