I have three python files file1.py
, file2.py
, file3.py
. Each file will generate a .csv
file and will give it to the other file sequentially. To elaborate file1.py
will generate file1.csv
and this .csv
file will be the input of file2.py
and so on.
import file1
import file2
import file3
file1
file2
file3
IOError: File file2.csv does not exist
The problem is when I import file2.py
,there is no file1.csv
as it is yet to be executed. Please let me how to call each one sequentially without even reading the next python file.
Each of your scripts should be set up as a function or class that generates the file. The general structure could be something like this:
# file1.py
def generate_csv(filename):
# put your file generation code here
# you could easily use Python's csv module, for example
return csv_filename
Then in your main script you can call each of your scripts, like this:
# main.py
import file1, file2, file3
def main(filename):
fname_one = file1.generate_csv(filename)
fname_two = file2.generate_csv(fname_one)
fname_three = file3_generate_csv(fname_two)
This keeps your original scripts from being run when imported. Your main script controls the order of execution and can do whatever needs to be done to the 3rd file name that is returned.