Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8966529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:02:32+00:00 2026-06-15T17:02:32+00:00

I am trying to take in a filename from a user and then use

  • 0

I am trying to take in a filename from a user and then use execfile() to execute the file. Below is my code:

print "Please enter the name of the file"
filename = raw_input().split(".")[0]
module = __import__(filename)
execfile(module)             <-- this is where I want to execute the file

I understand that execfile() works as follows:

execfile("example.py")

I am unsure on how to do this when the filename is passed as a variable . I am using python 2.7.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:02:33+00:00Added an answer on June 15, 2026 at 5:02 pm

    Drop the import and exec the filename. You need the .py extension with this method and it will run any if __name__ == '__main__' section:

    filename = raw_input("Please enter the name of the file: ")
    execfile(filename)
    

    If you just want to import it, you need to strip the ‘.py’ and it will not execute an if __name__ == '__main__' section:

    filename = raw_input("Please enter the name of the file: ").split(".")[0]
    module = __import__(filename)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to take in several file names from the user at the command line
I'm trying to print a query result to file from MySQL ( SELECT blah
I am trying this code to download a file from a Windows machine using
I am trying to figure out how to take an audio file from the
I am trying to take input from the keyboard and then store it in
I'm trying to use the built-in Internal.Transformation.Filename.Directory variable. I take as an example a
Hey I'm trying to take a String input from the user (in this case
I am trying to take the user to login page after cookies are cleared.
net I am trying to take a value from a previous form typically in
trying to pass in data from a file as a cmd line argument in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.