'Executing Python Program Within Another Python Program
Recently I have created a python program "program1" which makes changes to a text file and then prints the changes to a new text file.
Now, I have created a program "program2" which checks a directory for any files ending with a number. This program then executes the previous "program1" onto the specified files.
However, the problem I'm having is that "program1" only points to a specified file to make changes. I want "program2" to run "program1" onto different files.
For example, here is "program 1"'s code.
def my_main(ifile_name, ofile_name):
ifile_name = open(ifile_name, 'r')
lines = ifile_name.readlines()
ofile_name = open(ofile_name, "w")
start = 0
end = len(lines) - 1
while start < end:
ofile_name.write(lines[start])
ofile_name.write(lines[end])
start += 1
end -= 1
if start == end:
ofile_name.write(lines[start])
ifile_name.close()
ofile_name.close()
This program opens "ifile_name" which is a specified text file, then prints the changes to "ofile_name" which is another specified text file. I want "program 2" to execute "program 1" on the files that I will specify on "program 2".
So far, here is the attempt I have made. I am trying to get "program2" to open "program1" and edit the value for "ofile_name" to the value that the user will specify in "program2". Is this possible? Or are there better ways of achieving this? Thank you.
Code is as follows:
import os
import re
import program1
def my_main():
directoryname = raw_input("Please provide path to directory")
for filenames in os.listdir(directoryname):
if re.match(r'w*\d+.txt', filenames):
with open(program1, 'r')as file:
for ofile_name in file:
ofile_name = filenames
execfile(ex01)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
