Ask coding questions

← Back to all posts
How can I open a file based on user input?
ANDREWVOSS

In the code:

Is there any way I can import the filename that the user inputs?

Answered by Coder100 [earned 5 cycles]
View Answer
Voters
jjrice2233
ANDREWVOSS
Comments
hotnewtop
Coder100

You actually can!

read this

Another way:

ANDREWVOSS

@Coder100 Oh, Thanks!

Coder100
ANDREWVOSS

@Coder100 So if I structure it as

How would I read the file?

Coder100

@ANDREWVOSS nooo
use it like import:

ANDREWVOSS

@Coder100 Sorry, let me rephrase that. How would I make it read the file that has been imported? eg:

where the other.py at the end outputs the file

ANDREWVOSS

@ANDREWVOSS nvm I found another way

Coder100

@ANDREWVOSS um thats really bad

ANDREWVOSS

@Coder100 fair enough

ANDREWVOSS

@ANDREWVOSS still working on it

Coder100

Anyways, you should treat __import__ literally as import @ANDREWVOSS

ANDREWVOSS

Okay... @Coder100

Coder100

@ANDREWVOSS so like

will work just fine (given you defined func)

Coder100

also

is invalid, you mean:

@ANDREWVOSS

ANDREWVOSS

@Coder100 Okay, I think I've got it. Thanks.

Coder100

great what did you do? @ANDREWVOSS

Coder100

yep that looks good @ANDREWVOSS

ANDREWVOSS

@Coder100 cool.

Coder100

mark as correct answer? @ANDREWVOSS

ANDREWVOSS

@Coder100

Is there a way to remove that error message?

ANDREWVOSS

@Coder100 there you go

Coder100

oh, type in file1 instead :) @ANDREWVOSS

Coder100

python is weird that way lol @ANDREWVOSS

Coder100

sorry for the late reply I was eating a sandwich @ANDREWVOSS

ANDREWVOSS

@Coder100 nah you're fine

ANDREWVOSS

@Coder100 thanks

zplusfour
InvisibleOne

As far as I know, you can't. I'm interested to know why you want to do this? What are you trying to make?

ANDREWVOSS

@InvisibleOne I was mostly just wondering, but I also wanted to make a program that could be used to seamlessly launch other programs from it.

InvisibleOne

Hmm, sounds interesting @ANDREWVOSS

ANDREWVOSS

@InvisibleOne I finished it

InvisibleOne
ANDREWVOSS

@InvisibleOne I think I'll post it in Share

ANDREWVOSS