Python Forum
[SOLVED] Retrieving the filename from a shell
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] Retrieving the filename from a shell
#1
Needing to find the latest filename in a specific directory. This is very basic but works

# Importing required module
import subprocess

# Using system() method to
# execute shell commands

subprocess.Popen('ls ~/Documents/Backups  -Artls | tail -1', shell=True)
Quote:960 -rw-r--r-- 1 ******** ******** 981156 Nov 28 08:01 Australian-2024-11-28.kmy

How to I retrieve that filename, so that I can then run 'gunzip' on the file and create the uncompressed file ? The gunzip part should be easy, it is retrieving that filename ?? Obviously STDOUT in some form. Also, I have read that using "ls" is not the best method ?
Reply
#2
Interesting - I tried the code at https://python-forum.io/thread-43593-pos...#pid182722 , modified it accordingly, and no output. Eventually it worked; the tilde (~) to signify 'home' path was not being recognised at all. The path name had to be explicitly defined.

my_command = 'ls /home/username/Documents/Backups  -Artls | tail -1'
works

my_command = 'ls ~/Documents/Backups  -Artls | tail -1'
doesn't work.
Reply
#3
Try this perhaps
from collections import deque
from pathlib import Path
import subprocess as sp


def last_entry(directory):
    d = Path(directory).expanduser()
    proc = sp.Popen(["ls", str(d), "-Artls"], stdout=sp.PIPE, encoding="utf8")
    dq = deque(proc.stdout, maxlen=1)
    proc.wait()
    return dq[-1] if dq else None


if __name__ == "__main__":
    x = last_entry("~/tmp")
    print(x)
Also you can simplify this by not using the 'rls' options in ls. Then you only take the first entry in the list instead of the last and get the file name directly
from pathlib import Path
import subprocess as sp


def last_entry(directory):
    d = Path(directory).expanduser()
    proc = sp.Popen(["ls", str(d), "-At", "-1"], stdout=sp.PIPE, encoding="utf8")
    last = next(proc.stdout, None)
    proc.stdout.close()
    proc.wait()
    return last


if __name__ == "__main__":
    x = last_entry("~/tmp")
    print(x.strip())
jehoshua likes this post
« We can solve any problem by introducing an extra level of indirection »
Reply
#4
Great, thank you @Gribouillis . Both of those scripts work just fine.

The first returns

Quote:960 -rw-r--r-- 1 ******** ******** 981156 Nov 28 19:16 Australian-2024-11-28.kmy

and the second returns

Quote:Australian-2024-11-28.kmy

so I can now use the second one as the filename to "gunzip"
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  KeyError while retrieving ESPN data john317ab 2 2,053 Nov-29-2023, 09:07 PM
Last Post: john317ab
  .get() not retrieving value? Sedos101 2 1,341 Aug-25-2023, 11:48 AM
Last Post: deanhystad
  [SOLVED] [Windows] Converting filename to UTF8? Winfried 5 4,794 Sep-06-2022, 10:47 PM
Last Post: snippsat
  [Solved] Retrieving a pdf from sqlite3 BigMan 4 4,128 Mar-12-2022, 01:56 PM
Last Post: deanhystad
  Retrieving a column from a data set using a function Bayle 6 4,122 Oct-06-2021, 08:52 PM
Last Post: Bayle
  Retrieving Cookies whois1230 2 2,980 Nov-21-2020, 12:01 PM
Last Post: snippsat
  Problem: Retrieving Form data PythonDev 3 4,243 Oct-16-2020, 02:09 AM
Last Post: PythonDev
  Retrieving dictionary keys within with another dictionay bazcurtis 8 4,258 Oct-29-2019, 10:06 PM
Last Post: bazcurtis
  Retrieving items from JSON bazcurtis 12 6,793 Oct-27-2019, 05:18 PM
Last Post: bazcurtis
  Trouble retrieving dictionary from mysql.connector cursor swechsler 2 4,266 Sep-17-2019, 05:21 PM
Last Post: swechsler

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020