Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python open file relative to script location

import os

path = 'a/relative/file/path/to/this/script/file.txt'

with open(os.path.join(os.path.dirname(__file__), path), 'r') as input_file:
    content = input_file.read()
Comment

PREVIOUS NEXT
Code Example
Python :: how to switch driver in python selenium 
Python :: python regex match words 
Python :: sqlite check if table exists 
Python :: python glob all files in directory recursively 
Python :: difference of two set in python 
Python :: python check if two sets intersect 
Python :: python sentence splitter 
Python :: pandas replace string with another string 
Python :: convert url to base64 image py 
Python :: Issue TypeError: can’t multiply sequence by non-int of type str 
Python :: opencv dilate 
Python :: where are python libraries installed in windows 
Python :: show all urls django extensions 
Python :: delete directory if exists python 
Python :: subsetting based on column value with list 
Python :: how to catch ctrl c in python 
Python :: how to hide a widget in tkinter python 
Python :: Read text file line by line using the readline() function 
Python :: get a colomn of csv in pandas 
Python :: python function as parameter 
Python :: python log10 
Python :: python3 strip punctuation from string 
Python :: object literal python 
Python :: excel get unique values from column formula 
Python :: show multiple plots python 
Python :: how to save the model in python 
Python :: install python 3.7 centos 
Python :: check if path exists python 
Python :: python sort dictionary by key 
Python :: strip array of strings python 
ADD CONTENT
Topic
Content
Source link
Name
7+7 =