views:

229

answers:

3

can you convert this perl code to python code :

$list = $ARGV[0];   
open (PASSFILE, "$list") || die "[-] Can't open the List of password file !"; 
@strings = <PASSFILE>; 
close PASSFILE;  

Thanks

+8  A: 

According to this reference and this one, something like this should theoretically work...

import sys
filename = sys.argv[1] # argv[0] is the program name, like in C
f = open(filename, 'r')
strings = f.readlines()
f.close()

I don't actually know any Python, so I'll just mention that Google is your friend and you could have done exactly what I did in less time than it took you to answer the question. Sure, my syntax might be off, but you could have learned that and used the diagnostics from the Python interpreter to figure out the proper syntax.

You should accept more answers for your open questions.

EDIT: Sorry, lots of typos. I wasn't really motivated to do a syntax check when (a) I didn't know Python and (b) the questioner could have done this himself/herself and saved himself/herself a lot of time.

Platinum Azure
+1 Why the downvote? Sometimes the truth hurts.
Bob Kaufman
-1. This code is nonsense. You're missing an `import sys`; `//` is a division operator; you're opening `f` for writing rather than reading , and in the final line you merely reference `f.close` rather than calling it. That's an impressive number of bugs for 4 lines of code.
Mark Dickinson
I guess it could be seen as slightly unprofessional, in fairness. EDIT: And blatantly incorrect too. Yeah, I mentioned I don't know Python, oh well.
Platinum Azure
Code's fixed, at any rate.
Platinum Azure
+2  A: 

How about translating that to modern Perl?

use strict;
use warnings;

use IO::Handle;

my $pw_path = shift;

open my $pwh, '<', $pw_path
    or die "Error opening file '$pw_path' - $!\n";

my @strings = $pwh->getlines;

$pwh->close or die "Error closing file '$pw_path' - $!\n";

Or if you like autodie:

use strict;
use warnings;

use autodie;
use IO::Handle;

my $pw_path = shift;

open my $pwh, '<', $pw_path;

my @strings = $pwh->getlines;

$pwh->close;
daotoad
I sure wish `autodie` was faster. If it were on par with the normal Perl builtins, the p5p's might consider making it the default behavior. Ah well.
Robert P
Robert: Can you elaborate on that? Is autodie slow? If so, how does it matter on system calls? Apart from that, it's never going to become the default behaviour as it would break a lot of code. In principle, it could become the default for all code following a "use 5.XXX", but that's unlikely, too. It took me a year or so to convince my way through the backwards-compat police to make "use 5.012" imply "use strict". But I'm quite proud to have saved about five trees of needless printouts of "use strict" by 2020 ;P
tsee