0

For some reason, my python code is writing to the wrong directory.

I'm setting the output directory I want to write to here:

output_dir = os.path.join('..', '..', 'output_files', 'aws_instance_list', 'csv')

And I'm setting the file name here:

output_file = output_dir + 'aws-instance-master-list-' + today + '.csv'

But when I write the file out, I get a file in this location with this name:

..\..output_files\aws_instance_list\csvaws-instance-master-list-11-19-2019.csv

Instead, I want the file to be called:

..\..output_files\aws_instance_list\csv\aws-instance-master-list-11-19-2019.csv

What am I doing wrong?

3
  • output_file = output_dir + 'aws-instance-master-list-' + today + '.csv' did you print out output dir? You should be able to spot what's wrong. This is also why make sure you keep constructing all paths using os.path.join or pathlib. Commented Nov 19, 2019 at 16:39
  • 2
    hint: output_dir does not contain a trailing "/". Commented Nov 19, 2019 at 16:39
  • @ParitoshSingh definitely second pathlib. Path(output_dir) / f'aws-instance-master-list-{today}.csv') reads very well Commented Nov 19, 2019 at 16:44

3 Answers 3

1

os.path.join doesn't join a "\" to the end. Try this:

output_dir = os.path.join('..', '..', 'output_files', 'aws_instance_list', 'csv', '') # add '' to the end 

output_file = output_dir + 'aws-instance-master-list-' + today + '.csv'

Adding the '' to the end of os.path.join will add an ending "\", which will separate your csv subdirectory and the filename.

Sign up to request clarification or add additional context in comments.

2 Comments

This works, but you should not use + for joining a directory and a path. output_file = os.path.join(output_dir. 'aws-instance-master-list-' + today + '.csv') would be the better option. In this case you could also remove , '' from the line aboce.
I totally agree, was just keeping it as close as possible to op's code
0

from doc of join method:

The return value is the concatenation of path and any members of *paths with exactly one directory separator (os.sep) following each non-empty part except the last, meaning that the result will only end in a separator if the last part is empty.

This means output_dir's last character is not / or \.

You may add it like:

output_file = output_dir + '\\aws-instance-master-list-' + today + '.csv' (Note the escape character, also see @gelonida's comment on why this is dangerous and you should avoid using it)

or better way is:

output_file = os.path.join(output_dir, 'aws-instance-master-list-' + today + '.csv')

The last one is better because you don't need to deal with operating system dependent path naming conventions.

2 Comments

do not use '\\' for joining paths. As far as I know this will fail on linux. '/' on the other hand works on both platforms. But the real answer is to never use '+' to join path strings. just use os.path.join()
alternatively use os.path.sep, which will be "\\" or "/" respectively
0
output_file = os.path.join(
    output_dir, 'aws-instance-master-list-' + today + '.csv')

As @Paritosh Singh mentioned. Never use + to construct file paths.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.