python overwrite and append to text file [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Iam new to python scripting.Please help me to open text file in overwrite and append mode
I want to overwrite text file which is already there for the first time looping starts and from next loop onwards , it should append the line to that text file.
Please help

Use this:
with open('/path/to/file', 'w') as outFile:
outFile.write("new contents\n")
That's it! Using with you do not even need to close the file yourself.

Related

How to open and edit a file with .pas format which can be opened by Text editor or Notepad++ in Python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
I would like to open a .pas file in Python, ( Generally, I can open the .pas by Text Editor to see what is inside). So, I am looking for the code to open and edit the file in Python.
To more clear, I would like to edit some variables in the file. So first I need to open it and then edit the file.
You can use the builtin open() function to open a text file:
with open('file.pas', mode='r') as f:
print(f.read())
Here are the docs

Can I lock an Excel file while I am reading it in my python function? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am reading an Excel file and making minor changes of updating some columns. During this process I do not want the Excel file to be tampered with or someone to open and make changes to it and once the code gets run completely it should be unlocked so that we can view the changes.
Thank you.
You can use portalocker to lock a file. Just open the file like f = open("test.xlsx", "r") and lock it with portalocker.lock(f, portalocker.LOCK_EX), you can unlock the file with portalocker.unlock(f). You won't be able to open or edit the file while it is locked.
Found in this question: Lock file for access on windows

What is the equivalent of Batch file syntax (*.* or *.csv) for 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 4 years ago.
Improve this question
I am trying to open a file with python where the filename is 2018_01_25_(filename).csv.
In batch files i used C:*.csv to open the csv file. I have tried this in python 2.7 but it doesnt work.
a = pd.read_csv("filename.csv")
i want to use, a = pd.read_csv("*.csv")
Is there a python equivalent to this?
As far as I know there isn't a one-liner for this, but you can get around it with the os package.
import os
for f in os.scandir():
if '.csv' == f[-4:]:
a = pd.read_csv(f)
break
This code will read the first .csv file into a.

how do you store a variable in an external file [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 4 years ago.
Improve this question
So, i'm on assignment at school where i have create my own dice rolling game, i have completed everything but all i have to do now is store the scores that the user gets into a external file. I am new and have no idea how to do this, so could anybody help me out? Thanks!
You can try this kind of code.
with open(file, "a") as f:
f.write("write your content" + "\n")
But there are plenty of pages describing how to write content into files. Did you try these examples?
Reading and writing files in Python.

Python compressing a simple text file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am a beginner in python. I'm not to sure how to compress a simple text file with the user's sentence. Is the easiest way to go about it by using ".compress()".I need to compress a file into a list of words from the user's input to recreate the original file. What does this mean?
import zlib
and do it to compress "file.txt"'s content, creating "compressedFile.zlib":
text = open("file.txt", "rb").read()
with open("compressedFile.zlib", "wb") as myFile:
myFile.write(zlib.compress(text))
and do it if you need to decompress "compressedFile.zlib" later and get its content:
compressedText = open("compressedFile.zlib", "rb").read()
decompressedText = zlib.decompress(compressedText)

Categories