Home > database >  How to read file and then make folders based on the names in the file in python
How to read file and then make folders based on the names in the file in python

Time:12-01

I'm currently working on a program that reads a given file and then makes folders based on the names in the file. The text file is located in the program directory. When I run my code it gives me this error: OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'duck\n' It also creates the files, but they are the letters of the first word in the text file rather than all the names. I'm very lost.

This is my code:

import os
import sys
my_file = open("new clients.txt", "r")
list = my_file.readlines()
print(list)
for items in list:
    os.mkdir(items)

The names within the text file are:

duck crane boris kat

CodePudding user response:

my_file.readlines() will include the newline character at the end of a line.

I think what you want instead, is the split method.

Simply change my_file.readlines() to my_file.read().split(), and you should be set.

  • Related