Python Forum
How to get file name without the full path details and without extension
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get file name without the full path details and without extension
#1
Hi,

I'm new to python. Created a simple program which does search and replace (string) for a list of binary files located in given input directory and i copy the each files after replacing the string to a output directory.

I need help on two items.

item:1 (cant get only file name)
Getting each file name only for the given input directory (without the path and extension)

item2: (need some suggestion to implement this)

after i get each file name only for the given input directory, i want to update corresponding output file with file name.

for example, the output file will have a line like this,

FILENAME "SOMEFILENAME",TYPE,0,165,"5"

So, i need to get the filename only and i need to update "SOMEFILENAME" in the above given line. I need to do this for each and every output files.

Below is my code i created:

tried item 1 but it prints full path followed by filename.extension but for item 2, i do not know how to implement this. suggestions please.

import os
from pathlib import Path

openfile = input('Enter the input file name: ')
outputfile = input('Enter the output file name: ')
originalName = input('Enter original  name: ')
newName = input('Enter new name to be modified: ')

# sourcepath = os.listdir('inputfiles/')
sourcepath = os.listdir(openfile)
for file in sourcepath:
    # print(openfile)
    # print(outputfile)
    # print(os.getcwd())
    inputfile = openfile + '/' + file
    # print('conversion is on-going for: ' + inputfile)
    with open(inputfile, 'rb') as inputfile:

        print("file name is : " , inputfile.name)     // trying to print file name only without extension //
        filedata = inputfile.read()
        i = 0
        i = filedata.count(b'originalName')
        destinationpath = outputfile + '/' + file
        filedata = filedata.replace(bytes(originalName, 'ASCII'), bytes(newName, 'ASCII'))
        with open(destinationpath, 'wb') as file:
            file.write(filedata)
Reply
#2
Here are a few options:
filename = Path(...)
assume filename = /.../.../myfile.txt
to get just filename: print(filename.name) result: myfile.txt
to get full path: print(filename.resolve()) result /.../.../myfile.txt
to get just the stem: print(filename.stem result myfile
to get suffix: print(filename.suffix result: .txt
to get absolute path broken into tuples: filename.resolve().parts
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  File path by adding various variables Mishal0488 2 1,019 Apr-28-2023, 07:17 PM
Last Post: deanhystad
  Script File Failure-Path Error? jerryf 13 3,436 Nov-30-2022, 09:58 AM
Last Post: jerryf
  How to add product details in exe generated by pyinstaller arex786 1 8,420 Oct-10-2021, 11:00 AM
Last Post: Sran012
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,193 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  Subprocess.Popen() not working when reading file path from csv file herwin 13 14,915 May-07-2021, 03:26 PM
Last Post: herwin
  How to make a test data file for the full length of definition? MDRI 6 3,529 Apr-16-2021, 01:47 AM
Last Post: MDRI
  Add file to sys.path permanently hcccs 5 8,337 Jan-31-2021, 11:26 AM
Last Post: hcccs
  find file not knowing the extension Leon79 6 3,065 Jul-07-2020, 04:44 PM
Last Post: Leon79
  Can't open/read txt file in C extension for Python Rad226 8 4,754 Jun-26-2020, 04:08 PM
Last Post: Rad226
  How to append a tuple full of records to a dbf file in Python? DarkCoder2020 4 3,705 May-29-2020, 02:40 PM
Last Post: DarkCoder2020

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020