相对路径在 Python 中不起作用
Relative path not working in Python
我的 Python 脚本无法在以下脚本中解析 Linux 服务器上的相对路径:
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'))
错误:
[Errno 2] No such file or directory: '/home/vcap/app/static/filecache/file.csv.D3e3D7aF'
但是,当我这样做时,它会起作用并将文件保存到我的脚本路径中。
conn.download_file('mytestbucket22', 'file.csv', 'file.csv')
我的文件夹和文件结构如下所示:
--script.py
--static
----filecache
如何将文件保存到文件夹filecache?谢谢
conn.download_file('mytestbucket22', 'file.csv', os.path.join(os.getcwd(), 'static', 'filecache', 'file.csv'))
上面使用的模块、常量和函数的文档参考:
os
and os.path
个模块。
__file__
常数
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
")
我会用
conn.download_file('mytestbucket22', 'file.csv', os.path.join(os.path.dirname(sys.argv[0]), 'static', 'filecache', 'file.csv'))
我的 Python 脚本无法在以下脚本中解析 Linux 服务器上的相对路径:
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'))
错误:
[Errno 2] No such file or directory: '/home/vcap/app/static/filecache/file.csv.D3e3D7aF'
但是,当我这样做时,它会起作用并将文件保存到我的脚本路径中。
conn.download_file('mytestbucket22', 'file.csv', 'file.csv')
我的文件夹和文件结构如下所示:
--script.py
--static
----filecache
如何将文件保存到文件夹filecache?谢谢
conn.download_file('mytestbucket22', 'file.csv', os.path.join(os.getcwd(), 'static', 'filecache', 'file.csv'))
上面使用的模块、常量和函数的文档参考:
os
andos.path
个模块。__file__
常数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 pathnamepath
")os.getcwd()
(returns "a string representing the current working directory")os.chdir(path)
("change the current working directory topath
")
我会用
conn.download_file('mytestbucket22', 'file.csv', os.path.join(os.path.dirname(sys.argv[0]), 'static', 'filecache', 'file.csv'))