Python3 does not accept file.name

1

I have a script where I want to get the name of each file along the first line inside each file and pass those files to a list within a txt.

#modifica titulos de arquivos
import os
import glob

s=("-")

arq=open("listapdf.txt","w")
for file in glob.glob('*.pdf'):
    f = str(file.name)
    fr = str(file.readline())
    arq.write(f+s+fr)
    arq.write("\n")
exception (RuntimeError, TypeError, NameError)
pass

in the output points the following error:

 h1k3rpath@h1k3rpath-Ubuntu:~/Downloads/recovery/sabadoroadsec/pdf$
 python3 lista.py Traceback (most recent call last):   File "lista.py",
 line 9, in <module>
     f = str(file.name) AttributeError: 'str' object has no attribute 'name'

I have already changed and refactored the call with str and no str

I tried to use the keyword instead of naming variant, tried some other things, but I still can not get around this error.

I'm not correctly using file.name or have another way of doing this?

    
asked by anonymous 28.05.2017 / 20:08

1 answer

1

According to the Python3 documentation ( link ) glob.glob returns a list of strings. Strings do not have the .name attribute.

This is the error in your code.

Instead of using

f = str(file.name)

Use only

f = file
    
28.05.2017 / 21:01