Uncategorized

function – The problem in running the code of a Python file with another file


I wrote this code in Python.
When I open and run this code with VS Code, there is no problem and it runs.
But when I open this program with CMD or another file, in the ‘read_txt_file’ function, my program doesn’t do from ‘txt_files = [ ]’ to ‘txt_files.append’ and a few lines after that it says: API_KEY is not defined.

What should I do to run this code with CMD without any not defined error?

if you know that please write the complete its script.

pathH = './Account'


def read_txt_file():
    txt_files = []
    for root, files in os.walk(pathH):
        for file in files:
            if file.endswith('.txt'):
                txt_files.append(os.path.join(root, file))
                
    for txt_file in txt_files:
        with open(txt_file, 'r') as file:
            lines = file.readlines()

        if len(lines) >= 8:
            API_KEY =  str(lines[3].strip())
            API_SECRET_KEY =  str(lines[4].strip())
            bearer_token =  str(lines[5].strip())
            ACCESS_TOKEN =  str(lines[6].strip())
            ACCESS_TOKEN_SECRET =  str(lines[7].strip())
            return bearer_token, API_KEY, API_SECRET_KEY, ACCESS_TOKEN, ACCESS_TOKEN_SECRET
        else:
            return 'The file has less than 8 lines.'

read_txt_file()

result = read_txt_file()
if isinstance(result, tuple):
    API_KEY,bearer_token , API_SECRET_KEY, ACCESS_TOKEN, ACCESS_TOKEN_SECRET = result
    print('API_KEY', API_KEY)
    print('bearer_token', bearer_token)
    print('API_SECRET_KEY:', API_SECRET_KEY)
    print('ACCESS_TOKEN', ACCESS_TOKEN)
    print('ACCESS_TOKEN_SECRET', ACCESS_TOKEN_SECRET)

I wrote this code in Python.
When I open and run this code with VS Code, there is no problem and it runs.
But when I open this program with CMD or another file, in the ‘read_txt_file’ function, my program doesn’t do from ‘txt_files = [ ]’ to ‘txt_files.append’ and a few lines after that it says: API_KEY is not defined.
What should I do?



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *