为什么 Python open() 函数不接受 "directory/filename" 作为参数?
Why is Python open() function not accepting "directory/filename" as an argument?
本地环境:Python3、Bottle、MacOs
远程环境:Python3,瓶子,Python任何地方
这适用于我的本地环境,但不适用于我的远程环境:
@route('/test')
def test():
'''Function tests file open issue.'''
with open('uploads/Project2.csv', 'r', newline='') as file:
content = ""
content = file.read()
return content
这适用于我的远程环境,但不适用于我的本地环境:
@route('/test')
def test():
'''Function tests file open issue.'''
with open('uploads', 'r', newline='') as file:
content = ""
content = file.read()
return content
在第一种情况下,我将 文件路径 传递给打开函数。如果我将 文件夹名称 传递给它 returns 这个错误:
IsADirectoryError: [Errno 21] Is a directory: 'uploads'
在第二种情况下,我将一个文件夹名称传递给打开函数。如果我传递 文件路径 它 returns 错误:
NotADirectoryError: [Errno 20] Not a directory: 'uploads/Project2.csv'
我很困惑。有什么想法吗?
首先你必须确定该路径在你的远程服务器上是否存在。
import os
os.path.exists(<your path>)
第二,
你不必声明你的内容变量,你可以声明它
像这样。
content = file.read()
第三,
"uploads" is a directory not a file. Provide a file name in your
directory like you have provided in your local environment. if
"upload" is not a subdirectory of your code directory, then provide
absolute path. like
upload = "/home/ubuntu/env/uploads/projects.csv"
在这两种环境中 "uploads" 是代码目录的子目录但是...
在本地环境中,相对路径就足够了:
"uploads/file"
在远程环境中需要绝对路径:
"/home/my_projects/project/uploads/file"
我认为这是因为 Bottle 在远程环境中是 WSGI 对象,但在本地环境中不是。
本地环境:Python3、Bottle、MacOs
远程环境:Python3,瓶子,Python任何地方
这适用于我的本地环境,但不适用于我的远程环境:
@route('/test')
def test():
'''Function tests file open issue.'''
with open('uploads/Project2.csv', 'r', newline='') as file:
content = ""
content = file.read()
return content
这适用于我的远程环境,但不适用于我的本地环境:
@route('/test')
def test():
'''Function tests file open issue.'''
with open('uploads', 'r', newline='') as file:
content = ""
content = file.read()
return content
在第一种情况下,我将 文件路径 传递给打开函数。如果我将 文件夹名称 传递给它 returns 这个错误:
IsADirectoryError: [Errno 21] Is a directory: 'uploads'
在第二种情况下,我将一个文件夹名称传递给打开函数。如果我传递 文件路径 它 returns 错误:
NotADirectoryError: [Errno 20] Not a directory: 'uploads/Project2.csv'
我很困惑。有什么想法吗?
首先你必须确定该路径在你的远程服务器上是否存在。
import os
os.path.exists(<your path>)
第二, 你不必声明你的内容变量,你可以声明它 像这样。
content = file.read()
第三,
"uploads" is a directory not a file. Provide a file name in your
directory like you have provided in your local environment. if
"upload" is not a subdirectory of your code directory, then provide
absolute path. like
upload = "/home/ubuntu/env/uploads/projects.csv"
在这两种环境中 "uploads" 是代码目录的子目录但是...
在本地环境中,相对路径就足够了:
"uploads/file"
在远程环境中需要绝对路径:
"/home/my_projects/project/uploads/file"
我认为这是因为 Bottle 在远程环境中是 WSGI 对象,但在本地环境中不是。