filepath

NameError: name '__file__' is not defined , os.path.dirname(os.path.abspath(__file__))

NameError: name '__file__' is not defined , os.path.dirname(os.path.abspath(__file__)) Question: I’m trying to use os.path.dirname(os.path.abspath(file)) for tracking my file txt but i don’t why its not working with me: import os print(os.getcwd()) # Get Working directory print(os.path.dirname(os.path.abspath(__file__))) file = open("Youness.txt") NameError: name ‘__file__’ is not defined. Asked By: Error 404 || Source Answers: There is a …

Total answers: 1

Storing the path to folders and inner folders

Storing the path to folders and inner folders Question: i’m having difficulties trying to read from sub-folders that are inside a folder. What im trying to do is: i have my path "C:Dataset" which has 2 folders inside them and inside both folders, i have person names that have pictures for example: "C:DatasetDrunkJohnDoePic1", "C:DatasetSoberJaneDoePic1". I …

Total answers: 1

Anaconda / Python: Change Anaconda Prompt User Path

Anaconda / Python: Change Anaconda Prompt User Path Question: I want to change my Anaconda Prompt User file path. Currently it is as follows: I want it to change to: C:Usersu354590 How do I do this? The current version of anaconda I have is: Python 3.6.3 |Anaconda, Inc.| (default, Oct 15 2017, 03:27:45) [MSC v.1900 …

Total answers: 7

Concatenate path and filename

Concatenate path and filename Question: I have to build the full path together in python. I tried this: filename= “myfile.odt” subprocess.call([‘C:Program Files (x86)LibreOffice 5programsoffice.exe’, ‘–headless’, ‘–convert-to’, ‘pdf’, ‘–outdir’, r’C:UsersADesktopRepo’, r’C:UsersADesktopRepo’+filename]) But I get this error SyntaxError: EOL while scanning string literal. Asked By: nina_berlini || Source Answers: Try: import os os.path.join(‘C:UsersADesktopRepo’, filename) The os module …

Total answers: 7

Python 3: an existing file not being identified using os.path.isfile function

An existing file not being identified using os.path.isfile function Question: I have a file 7.csv in directory: ‘~/Documents/Jane/analyst/test/1/’. I was able to read this file using pandas.read_csv function with no problem. f_path = ‘~/Documents/Jane/analyst/test/1/7.csv’ pd.read_csv(f_path, index_col=None, header=0) But when checking whether this file is exsiting using os.path.isfile(), the result return False. os.path.isfile(f_path) False What could …

Total answers: 3

How to get Desktop location?

How to get Desktop location? Question: I’m using Python on Windows and I want a part of my script to copy a file from a certain directory (I know its path) to the Desktop. I used this: shutil.copy(txtName, ‘%HOMEPATH%/desktop’) While txtName is the txt File’s name (with full path). I get the error: IOError: [Errno …

Total answers: 11

Unable to locate files with long names on Windows with Python

Unable to locate files with long names on Windows with Python Question: I need to walk through folders with long file names in Windows. I tried using os.listdir(), but it crashes with long pathnames, which is bad. I tried using os.walk(), but it ignores the pathnames longer than ~256, which is worse. I tried the …

Total answers: 3

Extract a part of the filepath (a directory) in Python

Extract a part of the filepath (a directory) in Python Question: I need to extract the name of the parent directory of a certain path. This is what it looks like: C:stuffdirectory_i_needsubdirfile.jpg I would like to extract directory_i_need. Asked By: Thalia || Source Answers: You have to put the entire path as a parameter to …

Total answers: 7

Why doesn't os.normpath collapse a leading double slash?

Why doesn't os.normpath collapse a leading double slash? Question: Under Unix, os.path.normpath collapses multiple slashes into single ones except when exactly two slashes appear that the start of the path. Why the exception? To illustrate, I get the following transformations: //double/slash/stays -> //double/slash/stays /double/slash//gone// -> /double/slash/gone/ double//slash//gone/ -> double/slash/gone ///triple/slash/gone -> /triple/slash/gone ////quad/slash/gone -> /quad/slash/gone …

Total answers: 1