简体   繁体   中英

Server does not process config.ini file for credentials

I have a problem with my script. I have one file config.ini that contains multiple credentials that I need for my code and it is written like this:

[some section]
username = ...
password = ...

[some other section]
username = ...
password = ...

To use the config.ini file, I use the following function in my test.py script:

def config(filename, section):
    # Creates a parser
    parser = ConfigParser()
    # Reads the file
    parser.read(filename)

    cred = {}
    if parser.has_section(section):
        params = parser.items(section)
        for param in params:
            cred[param[0]] = param[1]
    else:
        raise Exception('Section {0} not found in the {1} file'.format(section, filename))

    return cred

And I get my parameters:

params = config(filename='config.ini', section='some section')

In my computer the.py script runs normally, but when I upload it to a server it produces this error

Exception: Section some section not found in the config.ini file

I can not understand the difference, I have tried looking up the documentation about section headers and their format, but did not manage to find anything. I have checked that the files do not have typos and that the script that does not run on the server is a direct copy of the file that runs on my pc.

Would much appreciate your help!!

I solved the problem, I gave the path to my config.ini file as

os.chdir('/../')

the first frontslash was not needed because it takes you to the starting path. In the end, the error was due to the system not finding the file. To find the error I run

import sys
print(sys.path)

and I saw that the paths that where used for my file did not contain the path the config.ini was in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM