Hack Forums
[Python] printing output with command - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] printing output with command (/showthread.php?tid=5946203)



printing output with command - Mr.7 - Mar 1, 2019

how to print output with command in python. i am getting output in python but not getting idea to print that output in text file.
how to compare that output file with user defined text file????? help me please 😭


RE: printing output with command - lockhead - Mar 1, 2019

I'm not quite sure what your question is

To print to console
Code
print("Hello world")

To print to file:
https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files
Code
file_name = "test.txt"
with open(file_name, "w") as f:
    f.write("Hello world!")

To compare files the simplest way is:
Code
user_file = input("Please enter a file name: ")
with open(user_file, "r") as f:
    f1_data = f.read()
with open("test.txt", "r") as f:
    f2_data = f.read()
if(f1_data == f2_data):
    print("File contents is equal")
else:
    print("Files do not match")