Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python get current file location

import os
os.path.dirname(os.path.abspath(__file__))
Comment

file path current directory python

#Python 3

#For the directory of the script being run:

import pathlib
pathlib.Path(__file__).parent.resolve()

#For the current working directory:

import pathlib
pathlib.Path().resolve()

#Python 2 and 3

#For the directory of the script being run:

import os
os.path.dirname(os.path.abspath(__file__))

#If you mean the current working directory:

import os
os.path.abspath(os.getcwd())
Comment

python get path of current file

import pathlib
pathlib.Path(__file__).parent.absolute()
Comment

python get file path

import os

def read_file(file_name):
    file_handle = open(file_name)
    print file_handle.read()
    file_handle.close()

file_dir = os.path.dirname(os.path.realpath('__file__'))
print file_dir

#For accessing the file in the same folder
file_name = "same.txt"
read_file(file_name)

#For accessing the file in a folder contained in the current folder
file_name = os.path.join(file_dir, 'Folder1.1/same.txt')
read_file(file_name)

#For accessing the file in the parent folder of the current folder
file_name = os.path.join(file_dir, '../same.txt')
read_file(file_name)

#For accessing the file inside a sibling folder.
file_name = os.path.join(file_dir, '../Folder2/same.txt')
file_name = os.path.abspath(os.path.realpath(file_name))
print file_name
read_file(file_name)
Comment

python get file path

import PySimpleGUI as sg
sg.theme("DarkTeal2")
layout = [[sg.T("")], [sg.Text("Choose a file: "), sg.Input(), sg.FileBrowse(key="-IN-")],[sg.Button("Submit")]]

###Building Window
window = sg.Window('My File Browser', layout, size=(600,150))
    
while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event=="Exit":
        break
    elif event == "Submit":
        print(values["-IN-"])
Comment

PREVIOUS NEXT
Code Example
Python :: how to shutdown a windows 10 computer using python 
Python :: create models in django 
Python :: strip comma from string python 
Python :: how to import file from a different location python 
Python :: install sklearn-features 
Python :: multiple functions tkinter 
Python :: generate sha1 python 
Python :: password manager python 
Python :: pipilika search engine 
Python :: read a file and split the words python 
Python :: merge on row number python 
Python :: python dataframe remove header 
Python :: django rest framework 
Python :: palindrome rearranging python 
Python :: python list iterate in 1 line 
Python :: 1 line if statement python 
Python :: python print for loop one line 
Python :: find order of characters python 
Python :: noninspection access to protected member 
Python :: how to close a webpage using selenium driver python 
Python :: how to print x in python 
Python :: create text file in directory python linux 
Python :: sum of column in 2d array python 
Python :: port 5432 failed: timeout expired 
Python :: django creating calculated fields in model 
Python :: python find number of occurrences in list 
Python :: get first line of file python 
Python :: how to practise python 
Python :: how to open xml file element tree 
Python :: python numpy array to list 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =