Create or open file in python [closed] - python

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
In python, I want to open a file if already exists or create it if it doesnt exist. also i want to write to the file new contents on opening it without overwriting the existing contents of the file. How can I do it?

PEP8 suggests you to use:
with open('test.txt', 'a+') as f:
f.write( "Your new content" )
The with statement is better because it will ensure you always close the file, even if an exception is raised.
Example adapted from: http://docs.python-guide.org/en/latest/writing/style/#pep-8

You can use:
file = open('myfile.dat', 'a+')
Refer to this link for details:
http://docs.python.org/2/tutorial/inputoutput.html

Related

Need Help Opening, Parsing, and Closing a File in Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am Python newbie and I am trying to learn how to parse files but I really don't even know where to begin. I need to basically find a specific code inside the code to confirm the functionality of the run log supplied to me. I just need help in the steps to: Open a file, parse a file, and close a file on Python. If you need anymore info please let me know. I can't supply the code but I can try to give as much info as I can. Thanks!
Opening a file:
myLog = open("path/to/my/log", 'r')
Loop through the lines:
for line in myLog:
if foundMyThing(line):
print "Found it!"
Close it:
myLog.close()
Read the docs:
https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files
with open("myfile.myextension",r) as f:
for line in f:
<parse each line>

Assigning different variables to different lines of a text file in Python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
There were some other questions that were similar but none of them answered my question. I need a simple way to assign a different variable to each line, so I can use it later on.
Please keep it simple, it's for my controlled assessment and I need to remember all of it.
No, you absolutely do not want to do this. There is never any reason to be assigning an unknown number of variables.
You assign a single variable, a list, with each element equal to a line:
f = open('myfile.text')
contents = f.readlines()
although most likely, since that will include the newline character at the end of the line, you want to strip it off first:
contents = [line.strip() for line in f]

Python, Using regex to read a password [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am aware that you can use the pwd module on python to extract passwd structures for use; however, my question is as such:
if I read into my program a line such as
blah1:tVNIsQ0yDrLxM:16009:0:99999:7:::
or
blah2:$6$WVsjYh8e$5r2wvIaeiFI6CCFRw6stfbah0Q.wrcKITdmEDCvG2cNC4fXkVbgRiOdeCdU.WeD1NIyzLh/sXycXQFEQcNWsv/:16009:0:99999:7:::
how would I read just the section that reads "tVNIsQ0yDrLxM"?
Thank you
As long as there's no extra colons in there, this should work:
password = hash.split(':')[1]
split

I need to load an excel file into python 2.7 using an interface [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
in order to do some operations with it but I would like to do it from an interface in order to select the file instead of just running a script with the name of the file, as the file name will change every day.
You can use Tkinter askopenfilename :
from tkFileDialog import askopenfilename
path = askopenfilename()
f = open(path, 'r') # OR DO WHAT YOU WANT WITH PATH

CSV file to multiple sheet excel file python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a folder of CSV files that I need to edit. Each file has a blank line in the middle. I want to take everything under that blank line and put it into a new excel sheet while rewriting my CSV file to a xls file. How can I do this?
A quick Google search revealed the openpyxl library that you can use to create XLS files.
Reading CSV is built into Python.
The rest is a simple matter of programming :) If you run into a more specific problem, please feel free to post a new question.

Categories

Resources