Reputation: 3
I'm creating a similarity program that calculates the euclidean distance of images, I'm looking for user input so that if they want to use a portion of the code, they can choose so. in that case, a line (specifically 13 in dc2.py) needs to be changed to " ". How can i go about this?
I've attempted using the open function alongside .write
, opening a file though open(dc.py).read()
, and to no avail.
This converts the image into an array (program dc2.py):
import numpy as np
import imageio
from numpy import array
img = imageio.imread("Machine Screw.jpg")
data = array(img)
with open('test2.txt', 'w') as outfile:
np.savetxt(outfile, data_slice, fmt='%-7.2f')
exec(open("Halfer.py").read())
Here's the failed code to change the previous .py:
inp = input("Want to use halfer?: ")
if inp == 'y':
the_file = open('dc2.py', 'a')
the_file[13].write(' ')
I expected:
here's what actually happened:
Traceback (most recent call last):
File "C:/Users/User/Desktop/PySimCode/Resources/Ini.py", line 5, in <module>
the_file[13].write(' ')
TypeError: '_io.TextIOWrapper' object is not subscriptable
Thank you for all the help!
Upvotes: 0
Views: 167
Reputation: 2525
You can, but you shouldn't.
You are trying to activate some code based on uer imput, this is doable encapsulating the code in functions, that you can import and axecute based on a condition. You are trying to achieve this result by reading files and executing them manually, basically you are doing what the Python interpreter should do..
First, you need to chage you modules to something that activates at will, not as soon as the file is loaded, for instance your dc2.py would look like this:
import numpy as np
import imageio
from numpy import array
import Halfer # <- here you import the py file, not open and read
def run(use_halfer):
img = imageio.imread("Machine Screw.jpg")
data = array(img)
with open('test2.txt', 'w') as outfile:
np.savetxt(outfile, data_slice, fmt='%-7.2f')
if use_halfer:
Halfer.run()
..and your Halfer.py file should look like this:
def run():
# ..all your Halfer.py code here inside the run function
..and then your starting point of the script can look like:
import dc2
inp = input("Want to use halfer?: ")
dc2.run(inp == 'y') # <- here you tell dc2 to use halfer or not.
Upvotes: 0
Reputation: 11
Try this:
inp = raw_input("Want to use halfer?: ")
if inp == 'y':
origin_file = open('dc2.py','r').readlines()
the_file = open('dc2.py','w')
origin_file[12] = '\n'
for line in origin_file:
the_file.write(line)
the_file.close()
Some notes I'd like to add:
input
reads a block of text and parses it. You should probably always use raw_input
.open
does different things, depending on the mode parameter. In my case, I used r
for reading, then w
for writing. (I don't think there's a way to read and write on the same <open>
object). a
is append, which only lets you add lines. Read here<open>
, use .read()
or .readlines()
.'\n'
instead of ' '
..close()
on your <open>
when you are done with it!Hope this works for you!
Upvotes: 1
Reputation: 5329
Your solution what you want to implement is not too "Pythonic". In my opinion you should import dc2.py
file as module to Ini.py
script and use parameters based on user-input to manipulate the behavior of dc2.py
script.
For example:
dc2.py
import numpy as np
import imageio
import subprocess
from numpy import array
def image_converter(halfer_needed=True):
img = imageio.imread("Machine Screw.jpg")
data = array(img)
with open('test2.txt', 'w') as outfile:
np.savetxt(outfile, data, fmt='%-7.2f')
if halfer_needed:
sp = subprocess.Popen(["python", "Halfer.py"]) # Call Halfer.py script
ret_stdout, ret_stderr = sp.communicate() # These variables contain the STDOUT and STDERR
ret_retcode = sp.returncode # This variable conains the return code of your command
I think you want to call the Halfer.py
script if user wants it so I have used the subprocess module to call this script as you can see above. You can see more details and options about this module: https://docs.python.org/3/library/subprocess.html
Ini.py
from dc2 import image_converter # Import your function from "dc2.py" script
inp = str(input("Want to use halfer?: "))
if inp == 'y':
image_converter(halfer_needed=False)
image_converter() # you don't need to define the keyword argument because the default value is True.
Upvotes: 1