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

How to get the filesystem's root directory in Python?

Situation: I need to find the top level [root] directory of any operating system using the most Pythonic way possible, without system calls. Problem: While I can check for the operating system using things like if "Windows" in platform.system(), I…
Timothy Wong
  • 689
  • 3
  • 9
  • 28
6
votes
2 answers

Python: Check if a directory exists using os module

I'm trying to validate if a directory received as user input exists using the os module This is how I'm accepting the input: directory = input("Hi ! \n please type a directory, thanks !") The idea is that I want to make sure the user will type an…
CG_corp
  • 103
  • 1
  • 1
  • 8
6
votes
1 answer

How to quickly get the last line from a .csv file over a network drive?

I store thousands of time series in .csv files on a network drive. Before I update the files, I first get the last line of the file to see the timestamp and then I update with data after that timestamp. How can I quickly get the last line of a .csv…
user1367204
  • 4,549
  • 10
  • 49
  • 78
6
votes
3 answers

os.popen().read() - charmap decoding error

I have already read UnicodeDecodeError: 'charmap' codec can't decode byte X in position Y: character maps to . While the error message is similar, the code is completely different, because I use os.popen in this question, not open. I…
David
  • 139
  • 2
  • 8
5
votes
1 answer

What "command verbs" are available for the os.startfile 'operation' argument and what do they do?

According to the Python documentation, os.startfile takes two arguments: path and operation. Path is fairly well described and self-explanatory, but for operation, all that is said is: When another operation [(not 'open')] is given, it must be a…
Ian
  • 5,704
  • 6
  • 40
  • 72
5
votes
2 answers

python os.walk displays mixed windows and unix paths

I am trying to identify all files with certain names in a folder. I am using standard code to do that looking like this: for paths, subdirs, files in os.walk(start_dir, topdown=True): for file in files: print(os.path.join(paths,…
smoczyna
  • 489
  • 6
  • 18
5
votes
5 answers

Python restart program

I made a program that asks you at the end for a restart. I import os and used os.execl(sys.executable, sys.executable, * sys.argv) but nothing happened, why? Here's the code: restart = input("\nDo you want to restart the program? [y/n] > ") if…
claudio26
  • 149
  • 1
  • 1
  • 8
5
votes
4 answers

How to access file in parent directory using python?

I am trying to access a text file in the parent directory, Eg : python script is in codeSrc & the text file is in mainFolder. script path: G:\mainFolder\codeSrc\fun.py desired file path: G:\mainFolder\foo.txt I am currently using this syntax with…
Edmund Carvalho
  • 107
  • 1
  • 3
  • 11
5
votes
3 answers

Better way to find absolute paths during os.walk()?

I am practicing with the os module and more specifically os.walk(). I am wondering if there is an easier/more efficient way to find the actual path to a file considering this produces a path that suggests the file is in the original folder when…
Jordan
  • 902
  • 2
  • 10
  • 37
4
votes
2 answers

How to get all files modified in a certain time window?

I want to get all files modified/created in the last 1 hour with Python. I tried this code but it's only getting the last file which was created: import glob import os list_of_files = glob.glob('c://*') latest_file = max(list_of_files,…
user16773013
4
votes
3 answers

Rearranging nested directory

I have a folder system with the structure below: folderA - folder1 - file1A.txt - folder2 - file2A.txt - folder3 - file3A.txt folderB - folder1 - file1B.txt - folder2 - file2B.txt - folder3 - file3B.txt I wish to change the order…
Allentro
  • 406
  • 2
  • 13
4
votes
4 answers

KeyError with os.environ[] accessing variable from .env file

I'm trying to build a slackbot and retrieve the slack token from a separate .env file. When I run it, I get thrown an error that looks like this: raise KeyError(key) from None KeyError: 'SLACK_TOKEN' The code for the bot (ShoppingListBot.py) is…
fnaimi
  • 51
  • 1
  • 4
4
votes
2 answers

How do I get the child folder name of the path besides these methods?

Of the given path like "level1/level2/level3/", I'd like pass it through some operation and get the result like "level3/". So I made two trials like these: TRIAL 1: After finding parent property within the Path object, I looked for something close…
Kiran Racherla
  • 219
  • 3
  • 12
4
votes
2 answers

What is the purpose of creating a symbolic link between files?

Recently I came across the os library in Python and found out about the existence of symbolic links. I would like to know what a symbolic link is, why it exists, and what are various uses of it?
Varun Moorthy
  • 89
  • 1
  • 2
  • 5
4
votes
4 answers

Get name (not full path) of subdirectories in python

There are many posts on Stack Overflow that explain how to list all the subdirectories in a directory. However, all of these answers allow one to get the full path of each subdirectory, instead of just the name of the subdirectory. I have the…
Foobar
  • 7,458
  • 16
  • 81
  • 161
1
2
3
36 37