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
2
votes
2 answers

How do I successfully import something from a module which has the same name as a reserved function or keyword in Python?

in Python, I am trying to import the open function object from the built-in os module, but it would not let me import it as it has the same name as the built-in open function, which is used for opening files. How do I import an object/attribute from…
2
votes
4 answers

Get Windows Version in Python

when i type into the Console(CMD) "winver" i will get my windows version (The four numbers left of the build number, example: 1803,1903,1909,2004,20H2) But how can i get my windows version in python? i already tried: import…
Temal
  • 51
  • 1
  • 6
2
votes
1 answer

Using asyncio to run command in a time interval and terminate it afterwards

I'm excuting a shell command by os.system(). I planned to run it for 1 second, then terminate it if time exceeded. Here's what I tried instead as a test. import os, time, asyncio async def cmd(): os.system("my.py > my.txt") # this processes…
Lab
  • 196
  • 3
  • 18
2
votes
1 answer

Python run app and wait for it to load before interacting

I am trying to open an application and interact with it. I am facing an issue where I do open application, but due to its size it takes a while before it appears, therefore I'd like to catch the app object in a dynamic/pythonic manner. import…
Oskar_U
  • 472
  • 4
  • 13
2
votes
1 answer

Add to PATH for later use with os.system or subprocess.Popen in the same script

Let's say "D:\Temp\Subfolder\mytest.exe" is not in the PATH yet. I tried: import os, sys, subprocess sys.path.append("D:\Temp\Subfolder") # 1 os.environ['PATH'] += "D:\Temp\Subfolder" # 2 but in both cases, this…
Basj
  • 41,386
  • 99
  • 383
  • 673
2
votes
1 answer

Chdir to ~/Library on Mac is returning an error, regardless of current directory

My code: import os os.chdir("~/Library") The error: raceback (most recent call last): File "/Users/user/PycharmProjects/untitled/tree_creation.py", line 13, in os.chdir("~/Library") FileNotFoundError: [Errno 2] No such file or…
a1426
  • 256
  • 2
  • 8
2
votes
3 answers

find sub folders which contain images

What is the most efficent way to get path of subfolders which contain files. For example, if this is my input structure. inputFolder │ └───subFolder1 │ │ │ └───subfolder11 │ │ file1.jpg │ │ file2.jpg │ │ ... │ …
Sreekiran A R
  • 3,123
  • 2
  • 20
  • 41
2
votes
3 answers

Is my error due to an absolute path issue?

I am trying to create a variable that stores a folder within the directory I am working in called TimeSeries. After that, I am trying to read each file in TimeSeries. Apparently, my error stems from df = pd.read_csv(f) being a relative path instead…
Luck Box
  • 90
  • 1
  • 13
2
votes
2 answers

Delete a file from python script

I'm tying to delete a file using this code and I am getting "YES File is present" but on os.remove I am getting error. Code os.chdir('C:/Users/USERNAME/Desktop/WhatsApp Documents/7K1') if os.path.exists("file.jpg"): print('YES File is…
2
votes
3 answers

How to use all images read from a folder for image augmentation

I use the following code to read all images in a folder and use them for image augmentation. load_images() function reads all images as numpy array but when I use this function as input for image augmentation in the second part of the code, I get…
2
votes
1 answer

1 in every 10000 times or so I get a PermissionError when using os.remove()

I wrote a script where I'm extracting frames from a video to get some info, so I loop over the frames, write the image to a temporary folder, do some operations, delete the image: # if frame exists, capture while framesleft: framesleft,image =…
2
votes
1 answer

Cannot find module after change directory

So I have to create a run folder, that is, a folder which has a bunch of python files which I need to run. I am able to create this folder with ease and all the files are there. However, when I try to run the files using importlib, python is not…
Bryce Ramgovind
  • 3,127
  • 10
  • 41
  • 72
2
votes
2 answers

How to get the current working directory with os library and write a .txt file on it?

I want to find the current working directory (cwd) with os library and write .txt file on it. Something like this: import os data=["somedatahere"] #get_the_current_directory #if this_is_the_current_directory: new_file=open("a_data.txt", "a") …
Y4RD13
  • 937
  • 1
  • 16
  • 42
2
votes
3 answers

How to execute another python file and then close the existing one?

I am working on a program that requires to call another python script and truncate the execution of the current file. I tried doing the same using the os.close() function. As follows: def call_otherfile(self): os.system("python file2.py")…
OshoParth
  • 1,492
  • 2
  • 20
  • 44
2
votes
2 answers

Getting FileNotFoundError when trying to open a file for reading in Python 3

I am using the OS module to open a file for reading, but I'm getting a FileNotFoundError. I am trying to find all the files in a given sub-directory that contain the word "mda" for each of those files, grab the string in the filename just after…
Vaslo
  • 480
  • 1
  • 5
  • 22