os.path

Difference between os.path.dirname(os.path.abspath(__file__)) and os.path.dirname(__file__)

Difference between os.path.dirname(os.path.abspath(__file__)) and os.path.dirname(__file__) Question: I am a beginner working on Django Project. Settings.py file of a Django project contains these two lines: BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__)) I want to know the difference as I think both are pointing to the same directory. Also it would be great help if you could …

Total answers: 1

Open File in Another Directory (Python)

Open File in Another Directory (Python) Question: I’ve always been sort of confused on the subject of directory traversal in Python, and have a situation I’m curious about: I have a file that I want to access in a directory essentially parallel to the one I’m currently in. Given this directory structure: parentDirectory subfldr1 -testfile.txt …

Total answers: 6

Misunderstanding of python os.path.abspath

Misunderstanding of python os.path.abspath Question: 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:imagesimg1.jpg D:imagesimg2.jpg and so on But I get different result: D:codeimg1.jpg D:codeimg2.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 …

Total answers: 3

How to extract a file within a folder within a zip?

How to extract a file within a folder within a zip? Question: I need to extract a file called Preview.pdf from a folder called QuickLooks inside of a zip file. Right now my code looks a little like this: with ZipFile(newName, ‘r’) as newName: newName.extract(QuickLooksPreview.pdf) newName.close() (In this case, newName has been set equal to …

Total answers: 1

Python joining current directory and parent directory with os.path.join

Python joining current directory and parent directory with os.path.join Question: I want to do join the current directory path and a relative directory path goal_dir somewhere up in the directory tree, so I get the absolute path to the goal_dir. This is my attempt: import os goal_dir = os.path.join(os.getcwd(), “../../my_dir”) Now, if the current directory …

Total answers: 3

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

pros and cons between os.path.exists vs os.path.isdir Question: 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. Asked By: user1834048 || Source Answers: os.path.exists will also return True …

Total answers: 5

Python os.path.join() on a list

Python os.path.join() on a list Question: 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? Asked By: ATOzTOA || Source Answers: The problem is, os.path.join doesn’t take a list as argument, it has to be separate arguments. To …

Total answers: 5