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)
Related
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
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.
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.
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 5 years ago.
Improve this question
How to write a file in python so that all the content of the file is encoded in the form of secure hash(NOT MEANING FULL CODES) and I am able to decode the file and use the content of the file?
I am creating a project and I need a secure way to write a file which is encrypted and convert the content to non mean-able content.
And when I run my program I should be able to access the all the content of the files.
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.