Python Forum
os.listdir(path) and non-string as input
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
os.listdir(path) and non-string as input
#1
Dear Python Experts,

I am trying to interate through folders in my root directory.
With the start of the first interation (folder) a new folder should be created
named existingname+'_flip'.
I then want to look through the files of the original folder and copy them over to the new folder.
The copy code is not realized yet, for now I want to get the loop right but I struggle.


While I loop through "dirs" the following error comes:

for file_path in os.listdir(path):
TypeError: listdir: path should be string, bytes, os.PathLike or None, not tuple

It seems, I got the input type to the function wrong. How can I convert "path" to string and is
that really the right way?


def main():   
    #interate through tub folders
    rootdir = 'C:/Users/xxx/Downloads/xxx/tubs_J/thinktank/'

    for dirs in os.walk(rootdir):
    
         #create new folder = existing name + _flip
         outPath = os.path.join(dirs, '_flip')
         if not os.path.exists(outPath):
             os.makedirs(outPath)

         path = dirs
    
         #iterate through the names of contents of the folder
         for file_path in os.listdir(path):
        
             print(file_path)
             
             #write file then save    
             newpathwouldbe = os.path.join(outPath, file_path)
       
if __name__ == '__main__':
     main()
Reply
#2
According to the documentation, os.walk generates 3-tuples (dirpath, dirnames, filenames), so you need to write
for root, dirs, files in os.walk(rootdir):
    ...
root is a directory, dirs is a list of names of subdirectories and files is a list of names of subfiles. With your present code, dirs is a 3-tuple.
Reply
#3
Hi Gribouillis,

Thank you so much for your input!

I see, thanks for the hint.
I thought I can just leave the other two out if I only want the "dir". OK.
Now I am here:

TypeError: listdir: path should be string, bytes, os.PathLike or None, not list

Similar error.

dirs is not a string. How can I convert it to a string?

I tried os.listdir(str(path)) but I think I need the absolute path...I think
Reply
#4
Why is outpath not concatenating properly?

#interate through tub folders
rootdir = 'C:/Users/xxx/data_thinktank/'

for root, dirs, files in os.walk(rootdir):
flip = r'_flip'
outpath = os.path.join(str(dirs), flip)
print(outpath)

Output of print statement
['folder_1_xx']\_flip
Reply
#5
OK, got it.

outpath = os.path.join(rootdir, dir+"_flip")

now I a at this:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using string input for boolean tronic72 3 636 Nov-01-2023, 07:48 AM
Last Post: Gribouillis
Sad How to split a String from Text Input into 40 char chunks? lastyle 7 1,054 Aug-01-2023, 09:36 AM
Last Post: Pedroski55
  -i option changes sys.path (removes leading empty string '') markanth 6 1,901 Aug-26-2022, 09:27 PM
Last Post: markanth
Big Grin General programming question (input string)[ jamie_01 2 1,569 Jan-08-2022, 12:59 AM
Last Post: BashBedlam
  Convert string to path using Python 2.7 tester_V 10 6,278 Nov-20-2021, 02:20 PM
Last Post: snippsat
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,151 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  Creating new column with a input string drunkenneo 2 2,198 Apr-14-2021, 08:10 AM
Last Post: drunkenneo
  Python win32api keybd_event: How do I input a string of characters? JaneTan 3 3,730 Oct-19-2020, 04:16 AM
Last Post: deanhystad
  listdir on IP Adress OEMS1 3 2,836 Jul-19-2020, 06:01 PM
Last Post: bowlofred
  String to File Path creedX 4 3,215 Apr-06-2020, 07:29 PM
Last Post: creedX

Forum Jump:

User Panel Messages

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