search script - python

0

The problem is that I can not separate arquivo_novo from /root/arquivo_novo , as indicated below. I just need the "new_file" to be able to get into the LOOP IF and the system returns /root/arquivo_novo and never enters the LOOP IF . I can not consider /root/ because I do not know where the file will be.

>>> subprocess.call(["find / -iname arquivo*],shell=True)
>>> /root/**arquivo_novo**
>>> 0
>>>
>>> subprocess.check_output(["find / -iname arquivo*"],shell=True)
>>> b'/root/**arquivo_novo**\n'
    
asked by anonymous 13.12.2017 / 16:41

1 answer

0

The code below, #1 captures the output of the command as you suggested it yourself. We then use the #2 function of the os module to separate the base name from the absolute path. That is nothing more than to pass the absolute path to '/tmp/file.py'.split (' / ') [- 1]

>>> path = subprocess.check_output(["find / -iname arquivo*"], shell=True) #encontrado /tmp/file.py #1

>>> import os
>>> file_name = os.path.basename(path) #2

>>> file_name
>>> b'aquivo.py'
>>> #decodificar
>>> file_name.decode('utf-8')
>>> 'aquivo.py'
    
13.12.2017 / 19:07