Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relative path not working in Python

My Python script can't resolve the relative path on a Linux server in the following script:

import boto3
import os

conn = boto3.client('s3', region_name="eu-west-1", endpoint_url="https://example.com", config=Config(signature_version="s3", s3={'addressing_style': 'path'}))
conn.download_file('mytestbucket22', 'file.csv', os.path.join(os.getcwd(), 'static', 'filecache', 'file.csv'))

Error:

[Errno 2] No such file or directory: '/home/vcap/app/static/filecache/file.csv.D3e3D7aF'

However when I do it like this it works and it saves the file to the path of my script.

conn.download_file('mytestbucket22', 'file.csv', 'file.csv')

My folder and file structure looks like this:

--script.py
--static
----filecache

How can I save the file to the folder filecache? Thanks

like image 977
jz22 Avatar asked Mar 29 '26 20:03

jz22


1 Answers

conn.download_file('mytestbucket22', 'file.csv', os.path.join(os.getcwd(), 'static', 'filecache', 'file.csv'))

Documentation references for the modules, constants and functions used above:

  • The os and os.path modules.
  • The __file__ constant
  • os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
  • os.path.dirname(path) (returns "the directory name of pathname path")
  • os.getcwd() (returns "a string representing the current working directory")
  • os.chdir(path) ("change the current working directory to path")
like image 190
Saket Mittal Avatar answered Mar 31 '26 10:03

Saket Mittal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!