How can I open a file based on user input?
In the code:
inF = open(input("Enter Filename: "),'r') import inF inF
Is there any way I can import the filename that the user inputs?
Voters
zplusfour
inF = input('Filename:\n> ') try: with open(f'{inF}', 'r') as file: pass except: print('This file is not defined')
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
ANDREWVOSS
@InvisibleOne I think I'll post it in Share
You actually can!
read this
Another way:
@Coder100 Oh, Thanks!
np :D @ANDREWVOSS
@Coder100 So if I structure it as
How would I read the file?
@ANDREWVOSS nooo
use it like
import
:@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 nvm I found another way
@ANDREWVOSS um thats really bad
@Coder100 fair enough
@ANDREWVOSS still working on it
Anyways, you should treat
__import__
literally asimport
@ANDREWVOSSOkay... @Coder100
@ANDREWVOSS so like
will work just fine (given you defined
func
)also
is invalid, you mean:
@ANDREWVOSS
@Coder100 Okay, I think I've got it. Thanks.
great what did you do? @ANDREWVOSS
yep that looks good @ANDREWVOSS
@Coder100 cool.
mark as correct answer? @ANDREWVOSS
@Coder100

Is there a way to remove that error message?
@Coder100 there you go
oh, type in
file1
instead :) @ANDREWVOSSpython
is weird that way lol @ANDREWVOSSsorry for the late reply I was eating a sandwich @ANDREWVOSS
@Coder100 nah you're fine
@Coder100 thanks