0

Local environment: Python 3, Bottle, MacOs

Remote environment: Python 3, Bottle, Pythonanywhere

This works in my local environment but not in my remote environment:

@route('/test')
def test():
    '''Function tests file open issue.'''
    with open('uploads/Project2.csv', 'r', newline='') as file:
        content = ""
        content = file.read()
    return content

This works in my remote environment but not in my local environment:

@route('/test')
def test():
    '''Function tests file open issue.'''
    with open('uploads', 'r', newline='') as file:
        content = ""
        content = file.read()
    return content

In the first case, I pass a file path to the open function. If I pass a folder name to it returns this error:

IsADirectoryError: [Errno 21] Is a directory: 'uploads'

In the second case, I pass a folder name to the open function. If I pass a file path it returns error:

NotADirectoryError: [Errno 20] Not a directory: 'uploads/Project2.csv'

I am baffled. Any ideas?

0

2 Answers 2

2

First you have to be certain whether the path exists or not on your remote server.

import os 
os.path.exists(<your path>)

second, you dont have to declare your content variable, you can just declare it like this.

content = file.read()

Third,

"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"
Sign up to request clarification or add additional context in comments.

2 Comments

Found solution: "uploads" is a subdirectory of code directory but, in remote environment, needed full path to work. Thank you for your help.
dont use str, path_exists will be True or False depending upon the existence of path or not. open('uploads', 'r', newline=''), here you are trying to open the whole directory "uploads", please provide filename too, in this directory.
0

In both environments "uploads" is a subdirectory of the code directory but...

In the local environment a relative path was sufficient:

"uploads/file"

In the remote environment an absolute path was required:

"/home/my_projects/project/uploads/file"

I think that this is to do with Bottle being a WSGI object in the remote environment but not in the local environment.

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.