I want to generate dynamically the name of a yaml file based on the name of a python script. Generally this is my naming convention:
if test name is test_action_01.py
then the yaml file name should be action_01.yml
So to acheive that I have these two lines in the python script (inside test_action_01.py) :
file_name = (os.path.basename(__file__).replace("test_", "")).replace(".py", ".yml")
pb_path = os.path.abspath(os.path.join(os.path.dirname( __file__ ), '../playbooks', file_name))
with open(pb_path, 'r') as f:
pb = yaml.load(f)
When I run the script I get this strange error:
Traceback (most recent call last):
File "test_create_01.py", line 22, in setUp
with open(pb_path, 'r') as f:
IOError: [Errno 2] No such file or directory: '/data/jenkins/workspace/aws-s3-tests/aws-s3/playbooks/create_01.ymlc'
I don't understand from where the extra "c" character somes from ?
I'm using Python2.7.
2 Answers 2
You also have the test_action_01.pyc file that contains ( byte code ) , you need to check, maybe:
if not os.path.basename(__file__).endswith('c'):
...
Comments
cPython compiles its code to bytecode. This bytecode is what gets executed and it is stored in a *.pyc file, which allows this to be cached. So when you run your script, __file__ is actually test_action_01.pyc.
You can call pb_path.rstrip('c') to get rid of whether or not it is there (if there is nothing to remove, rstrip just returns the input string).