1

I'm trying to get only the file name without extension, what I still get this error even though it's what it says in the book I'm trying to read.

import os
import re
stringA =[fname.rsplit(' ',0)[0] for fname in os.listdir("C:\\Users\\Desktop\\New folder\\New folder\\")]
stringA1 = os.path.splitext(os.path.basename(stringA))[0]

I get this error:

~\Anaconda3\lib\ntpath.py in basename(p) 
    214 def basename(p): 
    215 """Returns the final component of a pathname""" 
--> 216 return split(p)[1] 
    217 
    218 
~\Anaconda3\lib\ntpath.py in split(p) 
    183 Return tuple (head, tail) where tail is everything after the final slash. 
    184 Either part may be empty.""" 
--> 185 p = os.fspath(p) 
    186 seps = _get_bothseps(p) 
    187 d, p = splitdrive(p) 
TypeError: expected str, bytes or os.PathLike object, not list
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Gagandeep
  • 29
  • 1
  • 3
  • Perhaps you need to apply the `[0]` to `stringA` inside the `basename()` function. – 9769953 Feb 15 '22 at 09:57
  • `stringA` is obvouisly a list (`[...]`). The error is pretty clear that `basename` expects a string, bytes or `PathLike` object, not a list... What are you trying to do? – Tomerikoo Feb 16 '22 at 12:10
  • Does this answer your question? [How to get the filename without the extension from a path in Python?](https://stackoverflow.com/q/678236/6045800) – Tomerikoo Feb 16 '22 at 12:13

3 Answers3

1

In the first line of your code - you want to split by a . and not by spaces as a . would be the separator between a filename and its extension.

Also, you want to pass a value of 1 to the maxsplit argument - meaning that you want at most 1 split. 0 means you don't want to split the input at all

stringA =[fname.rsplit('.',1)[0] for fname in os.listdir("C:\\Users\\Desktop\\New folder\\New folder\\")]
Mortz
  • 4,654
  • 1
  • 19
  • 35
  • But i have lots of file in this folder and just want to extract only file names of every file – Gagandeep Feb 16 '22 at 10:22
  • Like that [ 'Beed 1-2-22 to 7-2-22.xlsx', 'Doth_01 01 22_02 07 22.xlsx', 'Foadd.xlsx', 'Spy_010122_020722.xlsx', 'Viner_1.1.22_3.31.22_as of 2.8.22.xlsx', 'Vox Me_2.1.2022_2.7.22.xlsx'] – Gagandeep Feb 16 '22 at 10:28
1

To get all the the files in a folder only:-

import os
folder = "C:\\Users\\Folder"

for files in os.listdir(folder):
    filelist = (files.partition(".")[0])
    print(filelist)

To get all the files in folder and subfolders, use this:-

import os
folder = "C:\\Users\\Folder"

for root, dir, files in os.walk(folder):
    for file in files:    
        filelist = (file.partition(".")[0])
        print(filelist)
CodeWithYash
  • 223
  • 1
  • 15
0

If you want to obtain only the filename, without its extension and without the preceding path, you can combine the use of the os.path.splitext function with the split function from the string type:

>>> import os
>>> p = 'c/folder/folder/file.txt'
>>> os.path.splitext(p)[0].split('/')[-1]
'file'
Atalajaka
  • 125
  • 1
  • 2
  • 14