I run a command using subprocess. This is my code.
subprocess_using_popen.py
out_f = open("output_file.txt", "w")
file_path = "../Python_tests/display_alphabet_type.py"
cmd = ['python3', file_path]
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,\
#stdin=subprocess.PIPE,\
stderr=subprocess.STDOUT,\
universal_newlines = True
)
for line in proc.stdout:
sys.stdout.write(line) #on console
out_f.write(line) #to file
#out_f.write(stdout)
out_f.write("Return code: {0}\n". format(proc.returncode))
out_f.close()
Here, I take the input from keyboard. and I want this input to be written to the out file along with my stdout. How can this be done?
#display_alphabet_type.py
def display_digits_chars_symbols_in_string(str):
count_lowercase = 0
count_uppercase = 0
count_digit = 0
count_special_characters = 0
d = dict()
for char in str:
if char.islower():
count_lowercase += 1
elif char.isupper():
count_uppercase += 1
#elif char in range(0, 9):
elif char.isnumeric():
count_digit += 1
else:
count_special_characters += 1
d['count_lowercase'] = count_lowercase
d['count_uppercase'] = count_uppercase
d['count_digits'] = count_digit
d['count_special_chars'] = count_special_characters
return d
def main():
#str = input("Enter string: ")
print("Enter string: ")
string = input(str())
d = display_digits_chars_symbols_in_string(string)
print(d)
if __name__== "__main__":
main()
exit(0)
I get this output:
python3 subprocess_using_popen.py
a123#
Enter string:
{'count_digits': 3, 'count_uppercase': 0, 'count_lowercase': 1, 'count_special_chars': 1}
cat output_file.txt
Enter string:
{'count_digits': 0, 'count_uppercase': 0, 'count_lowercase': 1, 'count_special_chars': 0}
Return code: 0
Here, in the output_file, the entered string is not copied to the file as seen.How to get the keyboard entered string to this file?