os.path

Misunderstanding of python os.path.abspath

跟風遠走 提交于 2019-11-30 03:05:44
I have following code: directory = r'D:\images' for file in os.listdir(directory): print(os.path.abspath(file)) and I want next output: D:\images\img1.jpg D:\images\img2.jpg and so on But I get different result: D:\code\img1.jpg D:\code\img2.jpg where D:\code is my current working directory and this result is the same as os.path.normpath(os.path.join(os.getcwd(), file)) So, the question is: What is the purpose of os.path.abspath while I must use os.path.normpath(os.path.join(directory, file)) to get REAL absolute path of my file? Show real use-cases if possible. The problem is with your

Misunderstanding of python os.path.abspath

删除回忆录丶 提交于 2019-11-28 22:29:29
问题 I have following code: directory = r'D:\images' for file in os.listdir(directory): print(os.path.abspath(file)) and I want next output: D:\images\img1.jpg D:\images\img2.jpg and so on But I get different result: D:\code\img1.jpg D:\code\img2.jpg where D:\code is my current working directory and this result is the same as os.path.normpath(os.path.join(os.getcwd(), file)) So, the question is: What is the purpose of os.path.abspath while I must use os.path.normpath(os.path.join(directory, file))

pros and cons between os.path.exists vs os.path.isdir

夙愿已清 提交于 2019-11-28 15:55:13
问题 I'm checking to see if a directory exists, but I noticed I'm using os.path.exists instead of os.path.isdir . Both work just fine, but I'm curious as to what the advantages are for using isdir instead of exists . 回答1: os.path.exists will also return True if there's a regular file with that name. os.path.isdir will only return True if that path exists and is a directory. 回答2: Just like it sounds like: if the path exists, but is a file and not a directory, isdir will return False . Meanwhile,

Use os.listdir to show directories only

眉间皱痕 提交于 2019-11-28 12:19:20
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): if os.path.isdir(filename) == True: dirlist.append(filename) print dirlist Now this actually works if I input (via raw_input ) the current working directory. However, if I put in anything else, the list returns empty. I tried to divide and conquer this problem but individually every code piece works as intended. that's expected, since os.listdir only

Python os.path.join() on a list

荒凉一梦 提交于 2019-11-28 04:48:34
I can do >>> os.path.join("c:/","home","foo","bar","some.txt") 'c:/home\\foo\\bar\\some.txt' But, when I do >>> s = "c:/,home,foo,bar,some.txt".split(",") >>> os.path.join(s) ['c:/', 'home', 'foo', 'bar', 'some.txt'] What am I missing here? The problem is, os.path.join doesn't take a list as argument, it has to be separate arguments. This is where * , the 'splat' operator comes into play... I can do >>> s = "c:/,home,foo,bar,some.txt".split(",") >>> os.path.join(*s) 'c:/home\\foo\\bar\\some.txt' Assuming join wasn't designed that way (which it is, as ATOzTOA pointed out), and it only took two

Use os.listdir to show directories only

旧巷老猫 提交于 2019-11-27 06:57:17
问题 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): if os.path.isdir(filename) == True: dirlist.append(filename) print dirlist Now this actually works if I input (via raw_input ) the current working directory. However, if I put in anything else, the list returns empty. I tried to divide and conquer