How to change a file to be not executable through python? [duplicate] - python

This question already has answers here:
Changing file permission in Python
(9 answers)
Closed 5 years ago.
I need to change the permissions on a file so that it cannot be executable. I still need to be able to read it through open("filename", 'rb'). How can I do this in Python?

change it to a text file
a text file can't be executed but read

Related

File gets cleared when try to write to it [duplicate]

This question already has answers here:
How do I append to a file?
(13 answers)
Closed 6 years ago.
I am try to make it possible to save progress in my python game put the following line is giving me problems:
dataW = open("data.dat","wb")
How can I stop this line from clearing my file.
You're opening the file in write mode:
'w' for only writing (an existing file with the same name will be
erased)
Emphasis mine
You meant to use the append mode:
'a' opens the file for appending

How do I overwrite the text in a file using python3 so that the user can write more with out it interfering with old text? [duplicate]

This question already has answers here:
How to empty a file using Python
(2 answers)
Closed 6 years ago.
I'm confused and don't know what to do about this. I'm trying to overwrite the files text.
when opening a file with 'w' flag, it will rewrite the file if it exists.
with open('yourfile.ext', 'wt') as fileObj:
fileObj.write(stuff)

How to create a file (a text file as default if available) in python code [duplicate]

This question already has answers here:
Create empty file using python [duplicate]
(2 answers)
Closed 8 years ago.
This is the code I have so far, yes I know the 'dirs' code creates a folder, but I am not sure of the code (if there is one) to create a file instead!
import os
if os.path.isfile(outfile):
print("Name already used, please choose another")
else:
os.makedirs(outfile)
Any help would be appreciated :D
Inorder to create a file and work on it, use the open() function.
fp = open(outfile, mode)
modes:
r: read
w: if file exists, replace it with a new one
a: append existing file
In your case the mode is 'w'
For create a file you can just open a file in write mode
open('file.txt', 'w')
https://docs.python.org/3/library/functions.html#open

How to read a text file in reverse using python 2.2 [duplicate]

This question already has answers here:
How to read a file in reverse order?
(22 answers)
Closed 9 years ago.
Is there a way to read a text file in reverse for Python 2.2?
Thanks for the help =)
try the following.
# method not using the `reverse` function
def read_file_reversed(filename):
return open(filename, 'r').readlines()[::-1]
It reverses the list using slicing.
Be mindful that these will load the entire file into memory.

Convert a PDF to text with Python [duplicate]

This question already has answers here:
How to extract text from a PDF file?
(33 answers)
Closed 2 months ago.
How can I get the content of pdf file line by line in python? I have searched in stackoverflow but could not find any good answer. Notes: pyPdf gives assertion erro, if possible something with slate and pdfminer.
from the command line:python /path/to/pdf2txt.py -o text.txt /path/to/yourpdf.pdf
You can then just take the text file it makes and use for line in file:
If you want to be efficient you would have to change pdf2txt.py, and have outfp be a python iostring, which would avoid the making a file and then reading from it.

Categories

Resources