Sinisterly
Python writing & reading text - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: Python (https://sinister.ly/Forum-Python)
+--- Thread: Python writing & reading text (/Thread-Python-writing-reading-text)



Python writing & reading text - Lynux - 08-03-2014

Hello All,

Just wanted to share this simple snippet of code which allows you to read & write text files in Python, it's really easy and you have no need to import any librarys.

Code:
#!/usr/bin/python f = open('HC.log', 'w') f.write('Hello my name is Lynux!\n') f.write('Welcome to HackCommunity!') f.close() f = open('HC.log', 'r') print f.read()



RE: Python writing & reading text - Anima Templi - 08-03-2014

Sorry to say it, but this is completely useless. It's info that I consider as "general information" and is therefore not needed to share like that. Besides, you'll literally get more than a million hits on Google.

Another thing, you why not comment on the code you share? Otherwise, how do you expect people to understand that code you're sharing?

It's great that you want to contribute, but please consider what's relevant.


RE: Python writing & reading text - bluedog.tar.gz - 08-03-2014

And please center the text left Wink
Code text centered looks very weird.


RE: Python writing & reading text - Psycho_Coder - 08-03-2014

Added Source tag. Removed Centered Alignment.

@Lynux Please Read this before making a tutorial. I hope you will understand how to write tutorials Smile http://www.hackcommunity.com/Thread-Tutorial-How-to-write-high-quality-tutorials


RE: Python writing & reading text - Lynux - 08-03-2014

Ok sorry I just thought that someone might find it useful, I'm currently working on a script that will install various tools either automatically or via the users request so I will modify this post with that, at least that will probably be a little better than the above code.


RE: Python writing & reading text - h3r0 - 08-07-2014

Maybe a commented version would be useful..

Code:
#!/usr/bin/python # Opening file "HC.log" from current working directory in write mode # More information on file modes here # https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files f = open('HC.log', 'w') # Write a few strings to the end of file f.write('Hello my name is Lynux!\n') f.write('Welcome to HackCommunity!') # Close out out the open file process f.close() # Open file "HC.log" in read mode f = open('HC.log', 'r') # print out the results of the read() method # this should output the contents of the file "HC.log" print f.read()