-1

I'm trying to make a directory using a read file but it not worked.

x= open(r'C:\Users\Fast Computer\Desktop\k.txt', 'r')
for f in x:
    path=r'C:\Users\Fast Computer\Desktop'
    n=f.readline()
    path=os.path.join(path,n)
    os.mkdir(path)
martineau
  • 119,623
  • 25
  • 170
  • 301

2 Answers2

1

I don't know what your file contains, but..

In your example..

Line 2 is already reading the lines from your file.

Line 4 will fail, because you are trying to run the readline() command from a string.

Also reading the lines from your file contain the newline character, so you should strip them.

Example:

x= open(r'C:\Users\Fast Computer\Desktop\k.txt', 'r')
for f in x:
    path=r'C:\Users\Fast Computer\Desktop'
    n=f.strip()
    path=os.path.join(path,n)
    os.mkdir(path)
0

Can you provide the content of your text file ? os.mkdir() fails if the path requires recursive dir creation. Use instead os.makedirs()

Sylvain
  • 16
  • 3