简体   繁体   中英

Importing file (without extension) contents into python script as object

I have a file titled "passc" (no .txt, no .py, no extension at all) that contains a single line of code (its a token) similar to:
aABBccDd01234

Yes, there are no quotes or variable assignment in this file

I am trying to import this value into a python script and assign it to a variable (I think it should look something like this):

import passc

code = passc

I have tried variations of the following:

from passc import *
code = passc[0]

And I understand I'm importing a module not a direct object so it shouldn't have a subscriptable element, but I thought I might as well try it.

Without defining the value in "passc" file I can't figure out how to access the value.

Looking for suggestions on how to access this value without altering the "passc" file!

Do not complicate yourself using import passc or from passc import * of this way only we can import files with .py extension.
The fast solution to fix this problem is doing something like this:

# read file
my_file=open("passc","r")
# save code in a var
code = my_file.read()

print(code)
# aABBccDd01234

Cheers!

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