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

Extract filename from path in Python

filename = os.path.basename("path/to/file/sample.txt")
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

extract name of file from path python

from pathlib import Path
Path("/tmp/d/a.dat").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 get just the filename in python 
Python :: display np array as image 
Python :: convert pdf to docx python 
Python :: how do i print the entire array pthon jupyter 
Python :: convert pandas datetime to day, weekday, month 
Python :: pygame get mouse position 
Python :: numpy install 
Python :: print today time python 
Python :: cmd run ps1 file in background 
Python :: python how move file to directory 
Python :: django user form 
Python :: pandas read_csv ignore unnamed columns 
Python :: python dictionary sort in descending order 
Python :: print json python 
Python :: how to set learning rate in keras 
Python :: pandas insert column in the beginning 
Python :: install re package python 
Python :: stopwatch in python 
Python :: python pyodbc install 
Python :: pytorch tensor add one dimension 
Python :: pyspark filter not null 
Python :: remove whitespace around figure matplotlib 
Python :: docker compose command not found 
Python :: python alphabet 
Python :: how to get input in tkinter 
Python :: how to sort a list by the second element in tuple python 
Python :: pandas determine percentage of nans in column 
Python :: python run 2 functions at the same time 
Python :: pd.to_datetime python 
Python :: python jwt parse 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =