script search - python

Asked

Viewed 350 times

1

The problem is I can’t separate the arquivo_novo of /root/arquivo_novo, as indicated below. I only need the "new file" to be able to enter the LOOP IF and the system returns /root/arquivo_novo and never enters the LOOP IF. I can’t consider the /root/ because I don’t 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'

1 answer

0


The code below, #1 captures the output of the command as you suggested. We then use the #2 function of the module os to separate the base name of the absolute path. That is nothing more than passing 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'
  • Is there any book or material that guides the creation of scripts in linux?

  • Linux the Bible and fluent Python are great books.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.