'Run a file in the same folder, using a variable in python

I am trying to run an other file in the same folder, by using a variable as the name of the file. But it always tells me, that its invalid.

name = File

import name

What do i have to add, that it detects, that this is a variable?



Solution 1:[1]

If your file is a py file and the file in the same directory. Just import file

or if it is in subdirectory import folder.file

or if you just want to import a variable or a function from file import some_variable or from folder.file import some_variable

or import everything from file import *

If your file not a py file you have to open the file according to file type. You can not use import

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sametatila