Python: Refer a properties file from different location


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Python: Refer a properties file from different location
# 1  
Old 11-15-2019
Python: Refer a properties file from different location

Hi All,
I'm having a python script: test.py in /path/to/script/test.py
I'm using a properties file: test_properties.py (it is having values as dictionary{}) which is in same DIR as the script.

Sample Properties file:
Code:
params = {
'target_db'     :     'a1_db'
'src_db'           :      'src1_db'
'countryCode'   :    'cc'
'date_1'    : 'start_date'
'date_2'   : 'end_date'
}

My sample code:
==============
Code:
from test_properties import *

It is working fine.
//Issue//
Now I'm keeping the properties file in: /path/to/config DIR and referring it in my script.
I have tried:
==========
Code:
fileName1=os.path.join('/path/to/config','test_properties.py')
from fileName1 import *

It is giving error:
==============
Code:
ImportError: No module named fileName1

I'm using python version 2.6 (default with Linux as of now in all Non-PROD serves) and I have requested Admin to to upgrade/install python 3.4+

Please help.

Thanks,
Saptarshi
# 2  
Old 11-15-2019
Hmm.
Please post the output of your directory:

Code:
ls -la /path/to/config

# 3  
Old 11-15-2019
It wants the bare module name - not a pathname.
Set the module search path instead! Like in
Expand Python Search Path to Other Source - Stack Overflow
# 4  
Old 11-15-2019
Sure Neo. The o/p as below:

Old setup:
Code:
-rw-r----- 1 dev_user1 dev_staff   76 Nov 15 09:09 test.py
-rw-r----- 1 dev_user1 dev_staff 2382 Nov 15 09:11 test_properties.py
-rw-r----- 1 dev_user1 dev_staff 1478 Nov 15 09:11 test_properties.pyc

New setup:
Code:
-rw-r----- 1 dev_user1 dev_staff   76 Nov 15 09:09 test.py
-rw-r----- 1 dev_user1 dev_staff 2382 Nov 15 09:11 test_properties.py
-rw-r----- 1 dev_user1 dev_staff 1478 Nov 15 09:11 test_properties.pyc
drwxr-x--- 2 dev_user1 dev_staff 4096 Nov 15 09:12 config

inside ./config folder:
Code:
-rw-r----- 1 dev_user1 dev_staff 2382 Nov 15 09:13 test_properties.py

--- Post updated at 05:20 AM ---

Sure MadeInGermany,

Checking the link and I'll try the solution posted there.

~ Sapta
# 5  
Old 11-15-2019
So, I guess you are sure this is not a permission issue since only dev_user1 user and dev_staff group can read this file?

Seems like a wild shot... just checking in between working on mysql query testing.
# 6  
Old 11-15-2019
Yeah, no permission issue. Just I'm unable to refer the config file if I moved it from the script DIR to any other DIR.

Actually I'm planning to have a "standard folder" structure for production.
so,
all scripts will be in "scripts" folder
all config will be in "config" folder
all SQLs will be in "sqls" folder
all reports will be in "output" folder
and so on...

Then I did that and tried to test the scripts again and failing as the scripts were not able to refer the config files from different dir.
# 7  
Old 11-15-2019
Did you try hard coding the full path into your code for testing?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to find a existing file location and directory location in Solaris box?

Hi This is my third past and very impressed with previous post replies Hoping the same for below query How to find a existing file location and directory location in solaris box (1 Reply)
Discussion started by: buzzme
1 Replies

2. UNIX for Dummies Questions & Answers

[Solved] How to refer to input file in code?

This may be a dumb question, but googling is not giving me an answer. I'm trying to figure out how to refer to an input file in my code. Lets say i run a script in bash: "sh shellscript.sh inputfile" (Inputfile will be variable...whatever file i run the script on) I wanted to make... (5 Replies)
Discussion started by: legato22
5 Replies

3. UNIX for Dummies Questions & Answers

Hot to retrieve *.sql file names which we refer in .sh file.

Hi Guys, How to retrieve/get *.sql file names which we refer in all *.sh files. Can any one help me on this. Thanks, Kolipaka (3 Replies)
Discussion started by: lakshmanrk811
3 Replies

4. Shell Programming and Scripting

How to copy a file from one location to another location?

I have file file1.txt in location 'loc1'. Now i want a copy of this file in location 'loc2' with a new file called test.txt. Please help me how to do this in shell script. (1 Reply)
Discussion started by: vel4ever
1 Replies

5. Shell Programming and Scripting

File created in a different location instead of desired location on using crontab

Hi, I am logging to a linux server through a user "user1" in /home directory. There is a script in a directory in 'root' for which all permissions are available including the directory. This script when executed creates a file in the directory. When the script is added to crontab, on... (1 Reply)
Discussion started by: archana.n
1 Replies

6. Shell Programming and Scripting

reading in properties file

Hi Am new to this scripting stuff so bear with me. I got a script made now that reads in a properties file. The properties file is in the following format: 256= Bos, Sea, FRa 128= HEL I want to be able to read in each line of the file and split out the letter fields by the numbered field. This... (2 Replies)
Discussion started by: vsekvsek
2 Replies

7. Shell Programming and Scripting

Put one string from one location to another location in a file

Hi Everyone, I have 1.txt here a b c' funny"yes"; d e The finally output is: here a b c d e' funny"yes"; (1 Reply)
Discussion started by: jimmy_y
1 Replies

8. UNIX for Dummies Questions & Answers

Refer a remote file

I need to refer a remote(present on another unix server) directory from my unix machine as a local file. e.g. I have one directory D1 on 10.10.10.10 and i need to access files in this directory just like they are present on my unix machine 20.20.20.20. Is there any way out... i read a bit... (1 Reply)
Discussion started by: blackeyed
1 Replies

9. UNIX for Dummies Questions & Answers

File properties

Hi , I do have a line in my code as follows: if ] ; then ... else ... fi What does the -z does ? Similarly there is -s in some other part of the code. I guess there are many options like this.. Can anybody please tell what all options are available and what do they mean ? (2 Replies)
Discussion started by: risshanth
2 Replies

10. HP-UX

Depot file properties

Hi How can we identify the informations like Author, meta data, dependency and other information from a depot file? (1 Reply)
Discussion started by: sethumadhavan
1 Replies
Login or Register to Ask a Question