I have two folders named Student
and Faculty
in the directory /home/ubuntu/Desktop/Pythontraining
. I need to save totally 10 files inside Student
folder and 3 files inside the Faculty
folder.I need to do the same in another system where the Student
and Faculty
folders are present in different directory(say: /home/documents/college
).How to store the file into the respective folders on two different machines without hardcoding the path?

- 3,518
- 10
- 23
- 31

- 109
- 1
- 2
- 12
2 Answers
For this kind of problem, you have multiple solutions :
Create environment variable on each machine, and inside the script do stuff like this :
import os
student_path = os.environ['STUDENT_PATH']
faculty_path = os.environ['FACULTY_PATH']
print(student_path, faculty_path)
Personal opinion : I don't like to configure my scripts using environment variables as the ones you chose may be used by another software + it is always messy to debug
Use arguments
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-s", "--student")
parser.add_argument("-f", "--faculty")
args = parser.parse_args()
student_path = args.student
faculty_path = args.faculty
print(student_path, faculty_path)
And then call your script like this and adapt this line depending on the machine
python <yourscript> -s <student_path> -f <faculty_path>
Personal opinion : I use arguments when I want to control a small amount of parameters on my scripts to change its behavior (verbose, nb of cpus, ...).
Create a config file and use configparser
config.ini file
[Paths]
student_path=<path_on_machine>
faculty_path=<path_on_machine>
Usage on script :
import configparser
config = configparser.ConfigParser()
config.read('config.ini')
student_path = config.get('Paths', 'student_path')
faculty_path = config.get('Paths', 'faculty_path')
print(student_path, faculty_path)
And then deploy different config.ini
files on each machine (tools like ansible can help you to automate this)
Personal opinion : I use config files when I need to adapt parameters when deploying on new machines. I don't like to use arguments for this purpose as I don't want to specify the same values every time I use my script (often these kind of parameters don't have good default values).
Create a module
You can also create a module to store these parameters instead of a config file.
my_config.py
student_path="<path_on_machine>"
faculty_path="<path_on_machine>"
And then import it
script.py
import my_config
print(my_config.student_path, my_config.faculty_path)
I don't have any personal opinion on config files vs config modules. Read this if you want some elements of comparison.

- 4,946
- 1
- 13
- 24
You can use walk library for finding destination folder path. It's working best if you have only one folder for each search name:
import os
start = "/home/"
for dirpath, dirnames, filenames in os.walk(start):
found = False
for dirname in dirnames:
if dirname == 'Student':
full_path = os.path.join(dirpath, dirname)
found = True
break
if found:
break
Output:
/home/.../Student

- 5,510
- 2
- 15
- 38
-
What if the folder 'Student' is present in multiple directory? – Saravanan Selvam Jul 11 '19 at 06:05
-
I'd say that it is dangerous to use walk instead of config files. You cannot be sure of the behavior of your script (especially if you have multiple student directories) and you may override important files. Be cautious. – Corentin Limier Jul 11 '19 at 16:11