Re: [Tutor] subprocess.getstatusoutput : UnicodeDecodeError

2017-09-22 Thread Chris Warrick
On 22 September 2017 at 03:57, Evuraan  wrote:
 result = subprocess.run(["tail", "-400", "/tmp/pmaster.txt"], 
 stdout=subprocess.PIPE)
 result.returncode
> 0
 subprocess.getstatusoutput("file  /tmp/pmaster.txt",)
> (0, '/tmp/pmaster.txt: Non-ISO extended-ASCII text, with very long
> lines, with LF, NEL line terminators')


You’re still using the deprecated function.

>>> subprocess.run(['file', '/tmp/pmaster.txt'], stdout=subprocess.PIPE)
CompletedProcess(args=['file', '/tmp/pmaster.txt'], returncode=0,
stdout=b'/tmp/pmaster.txt: Non-ISO…\n')
>>> result = _  # underscore means result of previous line in interactive mode
>>> result.stdout
b'/tmp/pmaster.txt: Non-ISO…line terminators\n'
>>> result.returncode
0

And if you want to get a Unicode string (if output of command is your
system encoding, hopefully UTF-8):

>>> subprocess.run(['file', '/tmp/pmaster.txt'], stdout=subprocess.PIPE, 
>>> universal_newlines=True)
CompletedProcess(args=['file', '/tmp/pmaster.txt'], returncode=0,
stdout='/tmp/pmaster.txt: Non-ISO…\n')
>>> (_.stdout is an unicode string)

Also, going back to your original example: you should not be using
`tail` from within Python. You should not depend on tail being
available (it’s not on Windows), and there may also be version
differences. Instead of tail, you should use Python’s standard file
operations (open()) to accomplish your task.

[advertisement] Extra reading on security (shell=False) and the
necessity of calling subprocesses:
https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/
[/advertisement]

-- 
Chris Warrick 
PGP: 5EAAEA16
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need Help with install of Python!

2017-09-22 Thread George Fischhof
2017-09-20 2:18 GMT+02:00 Alan Gauld via Tutor :

> On 19/09/17 21:13, Larry Staley wrote:
> > Hello I am very new to Python just having installed Python Version 2.7
> onto
> > my windows 8.1 laptop.  I thought the install was successful and was
> > entering information for my first assignment when I received an
> unexpected
> > error.
>
> Where did you get your vesion of Python?
> If it was the standard distribution from python.org or
> activestate.com then it does not include any of the SciPy
> packages(*) and you need to install them separately.
>
> If you expect to be using mamny of these types of package
> you are best fetching a distribution  that includes them
> all, for example, Anaconda or Enthought
>
> (*)Pandas is part of the SciPy suite of third party add-ons.
>
> > I executed a Sheets command using an earlier generated getSheetNames
> > function that successfully was entered by me.
>
> When you define a function the code inside is not
> executed merely compiled into a function object
> ready for execution.
>
> > However, when I ran the Sheets command I received the following:
>
> Its only when you call the function that the code inside
> gets executed.
>
> > def getSheetNames(excelfile):
> > from pandas import ExcelFile
>
> Its normal in Python to put all imports at the top of
> the file rather than inside any functions. In this
> case you try to import pandas everytime you call
> the function and while its not a big overhead it
> mounts up if you were calling this inside a
> repeating loop.
>
> And if the import was outside the function you would
> pick up the import error earlier.
>
>  excelfile=r:"C:\Users\Larry
>  sheets=getSheetNames
> > (excelfile);sheets
>
> I'm not sure what you are doing with that
> final ;sheets. I assuyme trying to evaluate the result of the function?
>
> It would be normal to just print it:
>
> >>> print getSheetNames(excelFile)
>
> or, if you need to store the result:
>
> >>> sheets=getSheetNames(excelfile)
> >>> sheets
>
> Combining commands on a single line doesn't save
> much typing and makes debugging harder.
>
> > I have in addition included the actual Python I executed and received the
> > message.  It is attached in the file.
>
> attachments often get rejected by the mailer,
> if its not a huge file(>100 lines) just paste
> it into the email.
>
> Always include the full error trace too.
>
> > If I need to install Pandas, please indicate clearly how I do using my
> > current ver 2.7 python install.
>
> The easiest way is just to grab one of the
> all-inclusive Python distros mentioned above.
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>




Hi Larry,

If You just installed Python 2.7, maybe it would be a good idea to replace
it with Python 3.6. It will not solve Your problem, but support of Pyhon 2
will be finished in 2020, and that time You will have to learn Python 3
anyway. So it is better to start with it if You have no any special
requirement to use Python 2.
(Python 3 is more modern, and is the future)
;-)


@tutors
Hi Tutors,

I think we should encourage people new to Python to use Python 3 instaed of
Python 2 as this is the future. ;-)


BR,
George
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor