1

I have a file path like this:

file_name = full_path + env + '/filename.txt'

in which:

  • full_path is '/home/louis/key-files/
  • env is 'prod'

=> file name is '/home/louis/key-files/prod/filename.txt'

I want to use os.path.join

file_name = os.path.abspath(os.path.join(full_path, env, '/filename.txt'))

But the returned result is only: file_name = '/filename.txt'

How can I get the expected result like above? Thanks

Arnab Nandy
  • 6,472
  • 5
  • 44
  • 50
Ragnarsson
  • 1,715
  • 7
  • 41
  • 74

1 Answers1

5

Since your last component begins with a slash, it is taken as starting from the root, so os.path.join just removes everything else. Try without the leading slash instead:

os.path.join(full_path, env, 'filename.txt')

Note you probably don't need abspath here.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895