This question already has answers here:
How to identify which OS Python is running on?
(27 answers)
Closed 3 years ago.
I am making a python program which would act as windows cmd, but I need a code which will display Windows's version. [Highlighted one]
How to make it?
import sys
ver = sys.getwindowsversion()
print("{}.{}.{}".format(ver.major, ver.minor, ver.build))
Output :
10.0.18362
import platform
platform.version()
My output was '10.0.17134'
You can try this:
import os
os.system('ver')
Related
This question already has answers here:
Convert Microsoft Word document to PDF using Python
(3 answers)
Closed 28 days ago.
How do I go about converting a word file to a pdf in python? I use replit, so I realise that rules out all the options that require linux to install Office
Many thanks,
ideally, this is what i expect
input(DOCUMENT.docx)
document.docx.convert(pdf)
output(DOCUMENT.pdf)
Just confirmed that the following works in Linux:
import subprocess
import os
wordfile = "yourword.docx"
command = ["lowriter", "--headless", "--convert-to", "pdf", wordfile]
subprocess.call(command)
if os.path.exists("yourword.pdf"):
print("Done")
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.
This question already has answers here:
How can I find the current OS in Python? [duplicate]
(5 answers)
Closed 2 years ago.
The following code will generate different values, on the same Windows 10 OS, based on what version of python you are using:
import sys
info = sys.getwindowsversion()
major = info.major
On a Windows 10 machine, in python2: major == 6 (which is not the correct value)
On a Windows 10 machine, in python3: major == 10 (the correct value)
I'm in the unfortunate position of needing to use python2 here, but the values it is returning are not accurate. Is there a module, available in python2, that will give me the accurate values for windows version information?
Python 2:
>>> platform.platform()
'Windows-10-10.0.17134'
Python 3:
>>> platform.platform()
'Windows-10-10.0.17134-SP0'
Besides that, unlike sys.getwindowsversion, you can also run this on Linux:
>>> platform.platform()
'Linux-5.4.0-51-generic-x86_64-with-glibc2.29'
There is also platform.version() which gives you just the version without the OS name.
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 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])