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
3
votes
1 answer

Python3: File is being used by another process

Normally when submitting code here, I would try to only include the minimum amount of code to demonstrate the problem. However, I did not encounter this problem until the very end of writing the code, which made it almost impossible to debug the…
3
votes
2 answers

How to rename a file in python with different numbers in it?

I am trying to rename a list of files with names for example: This is File 132 (file no 132) to This is File 132. So what I want is to replace (file no *) with ''. How to implement this *, where ultimately I want to replace this particular place in…
subtleseeker
  • 4,415
  • 5
  • 29
  • 41
3
votes
1 answer

Last Modified time functions return 0.0 for existing valid files - `getmtime` + `stat.st__mtime`

I have a caching engine that checks for a file's last mod time. The engine has been working great, but recently my tests started to fail with one specific file. The getmtime() function (or stat.st_mtime) both return zero even though the file exists…
gtalarico
  • 4,409
  • 1
  • 20
  • 42
3
votes
2 answers

Python os.system: Order of commands

Why does calling the file a.py with content import os print('Hi') os.system('cat a.py') yield the following output and how can I get them to print in the correct order? $ python a.py import os print('Hi') os.system('cat a.py') Hi You see that the…
Michael H.
  • 3,323
  • 2
  • 23
  • 31
3
votes
2 answers

Pandas Vectorize Instead of Loop

I have a dataframe of paths. The task is to get the last modification time for the folder using something like datetime.fromtimestamp(os.path.getmtime('PATH_HERE')) into a separate column import pandas as pd import numpy as np import os df1 =…
MattR
  • 4,887
  • 9
  • 40
  • 67
3
votes
2 answers

Windows vs Linux file modes

On a windows machine, I am trying to get a file's mode using the os module in python, like this (short snippet): import os from stat import * file_stat = os.stat(path) mode = file_stat[ST_MODE] An example for the mode I got for a file is 33206.…
Ofer Arial
  • 1,129
  • 1
  • 10
  • 25
3
votes
2 answers

How to create directories and sub directories efficiently and elegantly in Python 2.7?

I am trying to create a bunch of directories and sub directories at a specific location in my PC. My process is something like this: Check if there's any directory with the same directory name. Skip if so. If not, create the directory and the…
Chris Aung
  • 9,152
  • 33
  • 82
  • 127
2
votes
1 answer

Why do I get "TypeError: open() missing required argument 'flags' (pos 2)" or "TypeError: an integer is required (got type str)" when opening a file?

If your question was closed as a duplicate of this, it is because you have code along the lines of: from os import * with open('example.txt', mode='r') as f: print('successfully opened example.txt') This causes an error message that says…
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
2
votes
0 answers

Alternatives to os.sched_getaffinity for macOS

Python's os.cpu_count() suggests using len(os.sched_getaffinity(0)). However, this is not available on my Mac: >>> len(os.sched_getaffinity(0)) Traceback (most recent call last): File "", line 1, in AttributeError: module 'os' has…
Intrastellar Explorer
  • 3,005
  • 9
  • 52
  • 119
2
votes
1 answer

Problem when trying to convert .docx files to .pdf

I'm trying to create a simple docx to pdf converter and it throws me this problem: Exception has occurred: com_error (-2147352567, 'Exception occurred.', (0, 'Microsoft Word', 'Sorry, we couldn’t find your file. Is it possible it was moved, renamed…
Chris 789
  • 41
  • 1
2
votes
1 answer

Exception has occurred: PermissionError [WinError 32] The process cannot access the file because it is being used by another process:

I'm getting the following error when I run my code (the error occurs at the *** line): Exception has occurred: PermissionError [WinError 32] The process cannot access the file because it is being used by another process:…
2
votes
1 answer

Python os.path.join incorrectly appending paths

The python os module seems to be incorrectly joining paths. To replicate, simply run the following code: import os p1 = "/1/2" p2 = "/3/4" print(os.path.join(p1,p2)) Which will print "/3/4". Is this the expected behaviour? I would expect it to…
Recessive
  • 1,780
  • 2
  • 14
  • 37
2
votes
3 answers

if command with python os path module

so.. this is my code : import os def line(): pathInput = input("Type the addres of your file") pathExists = os.path.exists(pathInput) if(pathExists == true): something() elif(pathExists == false): print('said path is…
MADMENTAL
  • 33
  • 5
2
votes
1 answer

Using openpyxl with lambda

Python rookie here. I have a requirement for which i have been researching for a couple of days now. The requirement goes as below. I have an S3 location where I have few excel sheets with unformatted data. I am writing a lambda function to format…
Ludwig
  • 782
  • 1
  • 8
  • 24
2
votes
1 answer

How to get the newest file from a particular folder in Python

I have two directory with .xlsb and .msg extension i.e. for monthly and weekly. I want to get the most recent file from each folder. I am using the same code for both, but I am getting error only for weekly folder. self.weekly_file =…
KKL
  • 41
  • 2