This question already has answers here:
Locking a file in Python
(15 answers)
Closed 2 years ago.
I am working on some project.
I want that I lock some file whenever I run the script.
So that noone can delete/modify the file.
Use os module for this purpose.
import os
file_path = 'D:\\file.txt' # your file path
os.system(f'echo y| cacls {file_path} /P everyone:n')
I hope you got it.
Related
This question already has answers here:
How to convert Telegram voice in a wave file in python
(2 answers)
Closed 3 years ago.
Not able to proceed ahead.
We get .oga file from record in Telegram app.
Which needs to be converted to wav/ogg file format for further processing.
Taking in consideration, that this answer is purely academic, I had not try it. You can use ffmpeg, and run it through python with subprocess.
import subprocess
src_filename = 'source.ext1'
dest_filename = 'output.ext2'
process = subprocess.run(['ffmpeg', '-i', src_filename, dest_filename])
NOTE: Looking around, I found this that may be the same that you asked.
This question already has answers here:
How can I check the extension of a file?
(14 answers)
How to check type of files without extensions? [duplicate]
(10 answers)
Closed 4 years ago.
Is there any way to retrieve a file type name in Python using the OS module?
An example made up command:
>>> os.file_type('txt')
Would return:
'Text Document'
Any help would be appreciated :)
Oscar.
For getting file type you need to check the extension of the file
I think this can help.
import os
if os.path.splitext(file)[1] == ".txt":
pritn 'Text Document'
For os related task you can look over this doc.
https://github.com/Projesh07/Python-basic/blob/master/python_os_module/python_os_module.py
This question already has answers here:
How do I execute a program or call a system command?
(65 answers)
Closed 8 years ago.
can we write a script in python that runs dos commands/dos mode?
Eg: I have a command
megarec.exe -cleanflash .1
which supposed to run only on dos mode.I do it manually.Now i need to write a script which automates the above procedure.can i do it?
import subprocess
ch=subprocess.Popen("megarec.exe -cleanflash .1",shell=True,stdout=subprocess.PIPE,stderr=subrocess.PIPE)
output,err=ch.communicate()
if output:
print "success:",output
else:
print "error:",error
This question already has answers here:
How do you properly determine the current script directory?
(16 answers)
Closed 8 years ago.
I've got a question.
How can a program get its own location?
For example, I've got a script ("script.py") on a path ("C:\Programs\script.py").
I want a function, which gives me the path. Like the following:
scriptdic() ==> "C:/programs/"
Thanks for coming answers.
this should do the trick. . .
import os.path
import sys
os.path.dirname(sys.argv[0])
This question already has answers here:
How to redirect output with subprocess in Python?
(6 answers)
Python: How do I redirect this output?
(2 answers)
Closed 8 years ago.
I'm using python subprocess library to run command line in python file.
After importing the library, I used following code to store the output
call(["python", "make.py", ">", "data"])
But for some reason, I didn't get data file
You have to modify the stdout , check the official document subprocess
import subprocess
my_output_file = open("/home/user/output", "a")
subprocess.call(["python", "hello.py"],stdout=my_output_file)
my_output_file.close()