Questions tagged [python-os]

The Python os module provides a portable way of using operating system dependent functionality. Use this tag for questions about using the "os" module.

The Python os module provides a portable way of using operating system dependent functionality.

Links

551 questions
1433
votes
11 answers

How do I move a file in Python?

How can I do the equivalent of mv in Python? mv "path/to/current/file.foo" "path/to/new/destination/for/file.foo"
David542
  • 104,438
  • 178
  • 489
  • 842
357
votes
5 answers

Difference between os.getenv and os.environ.get

Is there any difference at all between both approaches? >>> os.getenv('TERM') 'xterm' >>> os.environ.get('TERM') 'xterm' >>> os.getenv('FOOBAR', "not found") == "not found" True >>> os.environ.get('FOOBAR', "not found") == "not found" True They…
André Staltz
  • 13,304
  • 9
  • 48
  • 58
152
votes
3 answers

How to get folder name, in which given file resides, from pathlib.path?

Is there something similar to os.path.dirname(path), but in pathlib?
trainset
  • 2,079
  • 3
  • 13
  • 17
38
votes
3 answers

get script directory name - Python

I know I can use this to get the full file path os.path.dirname(os.path.realpath(__file__)) But I want just the name of the folder, my scrip is in. SO if I have my_script.py and it is located at /home/user/test/my_script.py I want to return "test"…
spen123
  • 3,464
  • 11
  • 39
  • 52
33
votes
2 answers

Is it safe to use os.environ.setdefault?

From my ipython shell, I see a method setdefault in os.environ but it is not documented. http://docs.python.org/library/os.html#os.environ. Is it documented somewhere else? def setdefault(self, key, failobj=None): if key not in self: …
balki
  • 26,394
  • 30
  • 105
  • 151
21
votes
2 answers

python os.walk to certain level

I want to build a program that uses some basic code to read through a folder and tell me how many files are in the folder. Here is how I do that currently: import os folders = ['Y:\\path1', 'Y:\\path2', 'Y:\\path3'] for stuff in folders: for…
MattR
  • 4,887
  • 9
  • 40
  • 67
11
votes
1 answer

Difference between multiprocessing.cpu_count and os.cpu_count

Both os and multiprocessing modules define a cpu_count function. os.cpu_count is documented as follows: Return the number of CPUs in the system. Returns None if undetermined. and multiprocessing.cpu_count's documentation says: Return the number…
Right leg
  • 16,080
  • 7
  • 48
  • 81
10
votes
1 answer

Can a jupyter notebook find its own filename?

Is it possible for a jupyter notebook to get the name of its own file, similarly to what we would do from a python script? os.path.basename(__file__) doesn't seem to work, at least for me on jupyterlab sys.argv[0] returns…
Ziofil
  • 1,815
  • 1
  • 20
  • 30
8
votes
5 answers

OSError: [Errno 30] Read-only file system: '/User'. macOS Catalina

I was coding sorter for downloads folder. I'm getting this error, I tried to change permissions: chmod: Unable to change file mode on Users: Operation not permitted import os from_dir = os.path.dirname('/Users/user/Downloads/') working_dir =…
8
votes
1 answer

How does the code example in the python os module documentation create a security hole?

The documentation for the os module makes the following assertion: Note Using access() to check if a user is authorized to e.g. open a file before actually doing so using open() creates a security hole, because the user might exploit the short time…
K. Nielson
  • 191
  • 9
8
votes
3 answers

Difference between os.getlogin() and os.environ for getting Username

Is there a difference between using os.getlogin() and os.environ for getting the current user's username on Linux? At different times I've seen someone recommend looking at the environment variables $USER or $LOGNAME, and other times os.getlogin()…
jpyams
  • 4,030
  • 9
  • 41
  • 66
8
votes
2 answers

How to prevent a race condition when multiple processes attempt to write to and then read from a file at the same time

I have the following code (simplified for clarity): import os import errno import imp lib_dir = os.path.expanduser('~/.brian/cython_extensions') module_name = '_cython_magic_5' module_path = os.path.join(lib_dir, module_name + '.so') code = 'some…
abcd
  • 10,215
  • 15
  • 51
  • 85
7
votes
2 answers

How to key press detection on a Linux terminal, low level style in python

I just implemented a Linux command shell in python using only the os library's low level system calls, like fork() and so on. I was wondering how I can implement a key listener that will listen for key (UP|DOWN) to scroll through the history of my…
mama
  • 2,046
  • 1
  • 7
  • 24
7
votes
3 answers

Python: Get value of env variable from a specific .env file

In python, is there a way to retrieve the value of an env variable from a specific .env file? For example, I have multiple .env files as follows: .env.a .env.a ... And I have a variable in .env.b called INDEX=4. I tried receiving the value of INDEX…
swing1234
  • 233
  • 1
  • 3
  • 13
7
votes
6 answers

How do I close a file opened using os.startfile(), Python 3.6

I want to close some files like .txt, .csv, .xlsx that I have opened using os.startfile(). I know this question asked earlier but I did not find any useful script for this. I use windows 10 Environment
Learnings
  • 2,780
  • 9
  • 35
  • 55
1
2 3
36 37