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])
Related
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:
What does a . in an import statement in Python mean?
(3 answers)
Closed 2 years ago.
Inside numpy.fft._pocketfft.py you can find import commands like:
from . import _pocketfft_internal as pfi
How would I interpret this line?
. means from current package. Hence, this line means:
from . import _pocketfft_internal as pfi
Import _pocketfft_internal from current package, you should be able to see a file named _pocketfft_internal.py in the current directory. You can study more about this from here
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')
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:
What does a . in an import statement in Python mean?
(3 answers)
Closed 7 years ago.
Is there a difference between these two import statements in python
1) from .models import Q
2) from models import Q
I am a little bit confused with the dot, because models.py and file.py(where import is executed) are in the same directory.
Any help will be appreciated.
The dot tells python that the module is in the current directory. Without the dot, python will look up the module following standard procedures (from the system path, python path, current directory etc.). You can read more about it here: https://docs.python.org/2/tutorial/modules.html, paying attention to the relative path part