Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python get filename from path

import os
print(os.path.basename(your_path))
Comment

get filename from path python

from pathlib import Path
>>> p = r"C:UsersshaliOneDrivecodesetTheory.pdf"
>>> Path(p).anchor
'C:'
>>> Path(p).parent
WindowsPath('C:/Users/shali/OneDrive/code')
>>> Path(p).name
'setTheory.pdf'
>>> Path(p).stem
'setTheory'
>>> Path(p).suffixes
['.pdf']
Comment

python get path of current file

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

getting the file path of a file object in python

>>> import os
>>> f = open('file.txt')
>>> os.path.realpath(f.name)
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 filename from path

print (os.path.basename("/path/to/file.txt"))
# file.txt
Comment

python os get path

import pathlib
pathlib.Path().resolve()
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

Find path to the given file using Python

import os
 
print('Absolute path of file:     ',
      os.path.abspath(__file__))
print('Absolute directoryname: ',
      os.path.dirname(os.path.abspath(__file__)))
Comment

PREVIOUS NEXT
Code Example
Python :: add system path python jupytre 
Python :: python how to get pixel values from image 
Python :: get current data with python 
Python :: change string list to int list python 
Python :: import excel python 
Python :: select multiple columns in pandas dataframe 
Python :: python color input 
Python :: append in a for loop python 
Python :: intersect in python list 
Python :: print last exceuted query python 
Python :: python make a dictionary 
Python :: pandas describe kurtosis skewness 
Python :: hstack in numpy 
Python :: how to clear ipython console 
Python :: feature importance plot 
Python :: python timer decorator 
Python :: Import "whitenoise.django" could not be resolved 
Python :: Action based permissions in Django Rest V3+ 
Python :: read a csv and plot in python 
Python :: how to convert decimal to binary python 
Python :: pandas create column if equals 
Python :: http client post python 
Python :: print 1to 10 number without using loop in python 
Python :: how to play video in colab 
Python :: create square matrix python 
Python :: python import file from parent directory 
Python :: mario cs50 
Python :: flask migrate 
Python :: create a blank image opencv 
Python :: Python Django Models Unique Rows 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =