0
import os
gsize = os.system('dir dir C:\Users\GNK\Documents')

Which will give out put as below. but i want to print only last 2 lines.

C:\Users\GNK>dir C:\Users\GNK\Documents
06/26/2017  04:24 PM            13,826 1.xlsx
07/03/2017  10:52 PM            15,068 2.xlsx
08/11/2017  05:59 PM         3,227,457 3.zip
08/14/2017  02:56 PM               527 4.sql
06/09/2017  02:36 PM             3,976 5.sql
06/09/2017  02:54 PM           601,580 6.zip
06/12/2017  07:59 PM       116,641,792 7.mp3
08/12/2017  09:25 AM        15,747,576 8.exe
06/16/2017  12:23 AM           286,829 9
08/19/2017  02:38 PM         1,211,008 10
07/21/2017  08:08 AM            98,776 11
08/03/2017  10:38 PM        10,427,892 11
06/04/2017  05:20 PM           196,126 12
05/21/2017  09:11 AM           196,126 13
06/04/2017  05:22 PM           232,964 14
05/21/2017  09:11 AM           232,964 15
07/07/2017  12:03 PM            13,077 16
07/07/2017  12:02 PM            13,076 v17t
07/06/2017  07:57 AM        49,400,720 18
05/23/2017  12:56 AM           551,522 19
             160 File(s)    921,950,736 bytes
               3 Dir(s)  21,563,650,048 bytes free

I want to print only last 2 lines. how can I do this. Please help

1 Answer 1

1

From the os.system doc you can read:

The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function. See the Replacing Older Functions with the subprocess Module section in the subprocess documentation for some helpful recipes.

So I think would be better for you do something like:

import subprocess
print(subprocess.check_output(['dir', 'C:\Users\GNK\Documents']).splitlines()[-2:])

The .splitlines()[-2:] part is to keep just the last two lines. You can join them to form a two line string if you want with:

str.join('\n', subprocess.check_output(['dir', 'C:\Users\GNK\Documents']).splitlines()[-2:])
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. I Tried that it was throwing an error. was not able to fix that. please find below that error Traceback (most recent call last): File "g_size.py", line 4, in <module> proc1 = subprocess.Popen('git.cmd dir') File "C:\Python27\lib\subprocess.py", line 390, in init errread, errwrite) File "C:\Python27\lib\subprocess.py", line 640, in _execute_child startupinfo) WindowsError: [Error 2] The system cannot find the file specified
@GNK From where you are running the script? I read in your error output 'git.cmd dir', pls try to run the script outside whatever git shell you have. The other thing to try is to add the shell=True parameter to the subprocess.check_output func.
Tried changing the function and ran outside the git cmd. still no luck. error as below. File "g_size.py", line 4, in <module> print(subprocess.check_output(['dir', 'C:\Users\gnk' 'shell=True']).splitlines()[-2:]) File "C:\Python27\lib\subprocess.py", line 212, in check_output process = Popen(stdout=PIPE, *popenargs, **kwargs) File "C:\Python27\lib\subprocess.py", line 390, in init errread, errwrite) File "C:\Python27\lib\subprocess.py", line 640, in _execute_child startupinfo) WindowsError: [Error 2] The system cannot find the file specified
print(subprocess.check_output(['dir', 'C:\Users\gnk' 'shell=True']) is not good ;) it should be like: print(subprocess.check_output(['dir', 'C:\Users\gnk'], shell=True). The shell=True is an extra parameter to the check_output func, not part of the first parameter (that is a list). I don't have a windows machine near of me now, that is whyI'm kind of blind here.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.