Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: expected str, bytes or os.PathLike object, not tuple with os.path.basename

Im having this error while opening mutiples files at once with tkinter on Python3.

My Code while opening a file(s):

def OpenFile():

  Tk().withdraw()
  filename = askopenfilenames(title='Choose Combolist', filetypes=[("Text Files", "*.txt")])

  if filename:
      TxtTimeName(filename)
      pass

  else:
      NoFileSelected()

  return filename



def TxtTimeName(filename):

      CurrentTime = strftime(" %H-%M-%S")
      TxtName = os.path.basename(filename)
      TxtName = TxtName.replace(".txt", " ")
      FullName = TxtName + CurrentTime + ".txt"

      return FullName

My code while using theese file(s):

  def MailToUser():

    filename = OpenFile()
    FullName = TxtTimeName(filename)

    ctypes.windll.kernel32.SetConsoleTitleW("TextTool | Made by VRX | Mode: Mail To User")
    sys.stdout.flush()

    try:
        os.mkdir('Mail to User')
    except Exception as E:
        pass


    f = open(str("./Mail to User/" + "Combined.txt"),"w+")

    StartTime()

    with open(filename, "r+", encoding="utf-8", errors="ignore") as file:
        for line in file:
            word = line.strip()
            firstDelPos = line.find("@")
            secondDelPos = line.find(":")
            stringAfterReplace = line.replace(line[firstDelPos + 0:secondDelPos], "")
            try:
                 f.write(stringAfterReplace)
            except Exception as E:
                 pass

     EndTime()

ERROR:

Traceback (most recent call last):
  File "C:\Users\VRX\OneDrive\Desktop\Desktop\Python Projects\combotool\combotool.py", line 494, in <module>
    Main()
  File "C:\Users\VRX\OneDrive\Desktop\Desktop\Python Projects\combotool\combotool.py", line 420, in Main
    TextSorter()
  File "C:\Users\VRX\OneDrive\Desktop\Desktop\Python Projects\combotool\combotool.py", line 82, in TextSorter
    filename = OpenFile()
  File "C:\Users\VRX\OneDrive\Desktop\Desktop\Python Projects\combotool\combotool.py", line 27, in OpenFile
    TxtTimeName(filename)
  File "C:\Users\VRX\OneDrive\Desktop\Desktop\Python Projects\combotool\combotool.py", line 38, in TxtTimeName
    TxtName = os.path.basename(filename)
  File "C:\Users\VRX\AppData\Local\Programs\Python\Python38\lib\ntpath.py", line 208, in basename
    return split(p)[1]
  File "C:\Users\VRX\AppData\Local\Programs\Python\Python38\lib\ntpath.py", line 177, in split
    p = os.fspath(p)
TypeError: expected str, bytes or os.PathLike object, not tuple

It gives me this error everytime even when im selecting one file, it works when I use askopenfilename instead of askopefilenames but I can only select 1 file.

like image 605
semenseller123 Avatar asked Jan 19 '26 19:01

semenseller123


1 Answers

The error is because of the following:

  • askopenfilenames returns a tuple of filenames, even if there's only one
  • you are passing the result of askopenfilenames to os.path.basename, but that function expects a single filename

That is why you get the error expected str, bytes, or os.PathLike object, not tuple: you are passing a tuple and the error message says you can't do that.

like image 189
Bryan Oakley Avatar answered Jan 21 '26 08:01

Bryan Oakley