Questions tagged [os.path]

Python module implementing common pathname manipulations

502 questions
12
votes
1 answer

Use os.listdir to show directories only

How can I bring python to only output directories via os.listdir, while specifying which directory to list via raw_input? What I have: file_to_search = raw_input("which file to search?\n>") dirlist=[] for filename in os.listdir(file_to_search): …
Felix
  • 123
  • 1
  • 1
  • 5
11
votes
2 answers

an os.path.join() that doesn't discard before leading slash?

Python's os.path.join has been described as "mostly pointless" because it discards any arguments prior to one containing a leading slash. Leaving aside for the moment that this is intentional and documented behaviour, is there a readily available…
matt wilkie
  • 17,268
  • 24
  • 80
  • 115
10
votes
1 answer

os.path.abspath vs os.path.dirname

These are equal in string value, but are they truly equal? What is going on where? import os path_name1 = os.path.abspath(os.path.dirname(__file__)) path_name2 = os.path.dirname(os.path.abspath(__file__)) print(path_name1) print(path_name2)
Mwspencer
  • 1,142
  • 3
  • 18
  • 35
9
votes
4 answers

How to extract base path from DataFrame column of path strings

There are several questions about string manipulation, but I can't find an answer which allows me to do the following—I thought it should have been simple... I have a DataFrame which includes a column containing a filename and path The following…
rdh9
  • 665
  • 2
  • 11
  • 20
8
votes
4 answers

How to get a list of installed windows fonts using python?

How do I get a list of all the font names that are on my computer's system?
Paitor
  • 251
  • 2
  • 18
8
votes
1 answer

Python pathlib: Resolve full path to symbolic link without following it

Let's say I have a link/home/me/folder/link that points to /home/me/target. When I call pathlib.Path("link").resolve() from /home/me/folder/, it will return the resolved path to the target, not the resolved path of the link. How can I get the…
kuropan
  • 774
  • 7
  • 18
7
votes
1 answer

why os.path.normpath does not remove the firsts //?

Why the first // are not removed ? The following code: import os os.path.normpath('//var//lib/') returns '//var/lib' not '/var/lib' Here the definition: normpath(path) '''Normalize path, eliminating double slashes, etc.'''
user2652620
  • 464
  • 2
  • 17
7
votes
1 answer

What's the difference between './' and '../' when using os.path.isdir()?

I'm new to python. And something is confusing me today. Under the path c:\python\, there are several folds. I edit a python script under this path, and run the code: for dir_name in os.listdir("./"): print dir_name print…
L.Bes
  • 73
  • 2
7
votes
2 answers

os.path.join in python returns 'wrong' path?

I have the following python os.path output from ipython import os.path as path path.join("/", "tmp") Out[4]: '/tmp' path.join("/", "/tmp") Out[5]: '/tmp' path.join("abc/", "/tmp") Out[6]: '/tmp' path.join("abc", "/tmp") Out[7]:…
6
votes
6 answers

How to insert a directory in the middle of a file path in Python?

I want to insert a directory name in the middle of a given file path, like this: directory_name = 'new_dir' file_path0 = 'dir1/dir2/dir3/dir4/file.txt' file_path1 = some_func(file_path0, directory_name, position=2) print(file_path1) >>>…
NeStack
  • 1,739
  • 1
  • 20
  • 40
6
votes
2 answers

Pycharm "unresolved reference" on join of os.path

After upgrade pycharm to 2018.1, and upgrade python to 3.6.5, pycharm reports "unresolved reference 'join'". The last version of pycharm doesn't show any warning for the line below: from os.path import join, expanduser May I know why? (I used…
Liu Yong
  • 515
  • 5
  • 16
6
votes
5 answers

How/where to use os.path.sep?

os.path.sep is the character used by the operating system to separate pathname components. But when os.path.sep is used in os.path.join(), why does it truncate the path? Example: Instead of 'home/python', os.path.join returns '/python': >>> import…
Gaurav Vichare
  • 1,143
  • 2
  • 11
  • 26
6
votes
2 answers

python os.path.exists reports False when files is there

Hi have an application which is sometimes reporting that a file does not exist even when it does, I am using os.path.exists and the file is on a mounted network share. I am on OSX Yosemite, python 2.7.9 and I have access rights to the file. Here's…
speedyrazor
  • 3,127
  • 7
  • 33
  • 51
6
votes
4 answers

Python raising NotADirectoryError

I'm trying to raise an error when a directory does not exist, before I open files in that directory. According to this response I should use the most specific Exception constructor for my issue, which I think is NotADirectoryError. But running the…
BoltzmannBrain
  • 5,082
  • 11
  • 46
  • 79
6
votes
1 answer

os.path.isdir returns false when folder exists?

I have the following code which checks if the directory exists def download(id, name, bar): cwd = os.getcwd() dir = os.path.join(cwd,bar) partial = os.path.join(cwd, id + ".partial") print os.path.isdir(dir) …
ddd
  • 4,665
  • 14
  • 69
  • 125
1
2
3
33 34