[Tutor] Web scraping using selenium and navigating nested dictionaries / lists.

2019-01-27 Thread mhysnm1964
All,

 

Goal of new project.

I want to scrape all my books from Audible.com that I have purchased.
Eventually I want to export this as a CSV file or maybe Json. I have not got
that far yet. The reasoning behind this is to  learn selenium  for my work
and get the list of books I have purchased. Killing two birds with one stone
here. The work focus is to see if selenium   can automate some of the
testing I have to do and collect useful information from the web page for my
reports. This part of the goal is in the future. As I need to build my
python skills up. 

 

Thus far, I have been successful in logging into Audible and showing the
library of books. I am able to store the table of books and want to use
BeautifulSoup to extract the relevant information. Information I will want
from the table is:

*   Author 
*   Title
*   Date purchased 
*   Length
*   Is the book in a series (there is a link for this)
*   Link to the page storing the publish details. 
*   Download link

Hopefully this has given you enough information on what I am trying to
achieve at this stage. AS I learn more about what I am doing, I am adding
possible extra's tasks. Such as verifying if I have the book already
download via itunes.

 

Learning goals:

Using the BeautifulSoup  structure that I have extracted from the page
source for the table. I want to navigate the tree structure. BeautifulSoup
provides children, siblings and parents methods. This is where I get stuck
with programming logic. BeautifulSoup does provide find_all method plus
selectors which I do not want to use for this exercise. As I want to learn
how to walk a tree starting at the root and visiting each node of the tree.
Then I can look at the attributes for the tag as I go. I believe I have to
set up a recursive loop or function call. Not sure on how to do this. Pseudo
code:

 

Build table structure

Start at the root node.

Check to see if there is any children.

Pass first child to function.

Print attributes for tag at this level 

In function, check for any sibling nodes.

If exist, call function again 

If no siblings, then start at first sibling and get its child.

 

This is where I get struck. Each sibling can have children and they can have
siblings. So how do I ensure I visit each node in the tree? 

Any tips or tricks for this would be grateful. As I could use this in other
situations.

 

Sean 

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


Re: [Tutor] python - files

2019-01-27 Thread Peter Otten
Cameron Simpson wrote:

> Mats has mentioned the modules getopt and argparse etc. These are
> primarily aimed at option parsing ("-v", "-o foo"). Your situation
> occurs _after_ the option parsing (in your case, there are no options).

Not argparse. The main advantage over optparse is its handling of positional 
arguments. Your custom logic 

>   def main(argv):
> cmd = argv.pop(0)   # collect the command word
> badopts = False
> # mandatory first argument
> if not argv:
>   print("%s: missing first argument" % cmd, file=sys.stderr)
>   badopts = True
> else:
>   first = argv.pop(0)
>   # optional second argument
>   if argv:
> second = argv.pop(0)# explicit argument 2, use it
>   else:
> second = None   # or some otherdefault
>   if argv:
> print("%s: extra arguments: %r" % (cmd, argv), file=sys.stderr)
> badopts = true
> if badopts:
>   print("%s: invalid invocation, aborting" % cmd, file=sys.stderr)
>   return 2
> ... work with first and second ...

can roughly be replicated with the two lines

parser.add_argument("first")
parser.add_argument("second", nargs="?")

A working script:

$ cat test.py
#!/usr/bin/python3
import argparse

def main():
parser = argparse.ArgumentParser()

parser.add_argument("first")
parser.add_argument("second", nargs="?")

args = parser.parse_args()

print("first:", args.first)
print("second:", args.second)

if __name__ == "__main__":
main()

$ ./test.py
usage: test.py [-h] first [second]
test.py: error: the following arguments are required: first

$ ./test.py -h
usage: test.py [-h] first [second]

positional arguments:
  first
  second

optional arguments:
  -h, --help  show this help message and exit

$ ./test.py ONE
first: ONE
second: None

$ ./test.py ONE TWO
first: ONE
second: TWO

$ ./test.py ONE TWO THREE
usage: test.py [-h] first [second]
test.py: error: unrecognized arguments: THREE

Argparse makes a usable standard command line interface easy to set up (if 
you need non-standard behaviour it gets a bit harder).

There is also a companion module argcomplete (not in the stdlib) that 
enables autocompletion.


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


Re: [Tutor] python - files

2019-01-27 Thread Cameron Simpson

On 27Jan2019 10:30, Peter Otten <__pete...@web.de> wrote:

Cameron Simpson wrote:

Mats has mentioned the modules getopt and argparse etc. These are
primarily aimed at option parsing ("-v", "-o foo"). Your situation
occurs _after_ the option parsing (in your case, there are no options).


Not argparse. The main advantage over optparse is its handling of positional
arguments.


I stand corrected.


Your custom logic

[...]

can roughly be replicated with the two lines

parser.add_argument("first")
parser.add_argument("second", nargs="?")
[... extended example ...]


Thank you!

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


Re: [Tutor] Web scraping using selenium and navigating nested dictionaries / lists.

2019-01-27 Thread Marco Mistroni
Hi my 2 cents. Have a look at scrapy for scraping.selenium is v good  tool
to learn but is mainly to automate uat of guis
Scrapy will scrape for you and u can automate it via cron. It's same stuff
I am doing ATM
Hth

On Sun, Jan 27, 2019, 8:34 AM  All,
>
>
>
> Goal of new project.
>
> I want to scrape all my books from Audible.com that I have purchased.
> Eventually I want to export this as a CSV file or maybe Json. I have not
> got
> that far yet. The reasoning behind this is to  learn selenium  for my work
> and get the list of books I have purchased. Killing two birds with one
> stone
> here. The work focus is to see if selenium   can automate some of the
> testing I have to do and collect useful information from the web page for
> my
> reports. This part of the goal is in the future. As I need to build my
> python skills up.
>
>
>
> Thus far, I have been successful in logging into Audible and showing the
> library of books. I am able to store the table of books and want to use
> BeautifulSoup to extract the relevant information. Information I will want
> from the table is:
>
> *   Author
> *   Title
> *   Date purchased
> *   Length
> *   Is the book in a series (there is a link for this)
> *   Link to the page storing the publish details.
> *   Download link
>
> Hopefully this has given you enough information on what I am trying to
> achieve at this stage. AS I learn more about what I am doing, I am adding
> possible extra's tasks. Such as verifying if I have the book already
> download via itunes.
>
>
>
> Learning goals:
>
> Using the BeautifulSoup  structure that I have extracted from the page
> source for the table. I want to navigate the tree structure. BeautifulSoup
> provides children, siblings and parents methods. This is where I get stuck
> with programming logic. BeautifulSoup does provide find_all method plus
> selectors which I do not want to use for this exercise. As I want to learn
> how to walk a tree starting at the root and visiting each node of the tree.
> Then I can look at the attributes for the tag as I go. I believe I have to
> set up a recursive loop or function call. Not sure on how to do this.
> Pseudo
> code:
>
>
>
> Build table structure
>
> Start at the root node.
>
> Check to see if there is any children.
>
> Pass first child to function.
>
> Print attributes for tag at this level
>
> In function, check for any sibling nodes.
>
> If exist, call function again
>
> If no siblings, then start at first sibling and get its child.
>
>
>
> This is where I get struck. Each sibling can have children and they can
> have
> siblings. So how do I ensure I visit each node in the tree?
>
> Any tips or tricks for this would be grateful. As I could use this in other
> situations.
>
>
>
> Sean
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Web scraping using selenium and navigating nested dictionaries / lists.

2019-01-27 Thread Peter Otten
mhysnm1...@gmail.com wrote:

> All,
> 
>  
> 
> Goal of new project.
> 
> I want to scrape all my books from Audible.com that I have purchased.
> Eventually I want to export this as a CSV file or maybe Json. I have not
> got
> that far yet. The reasoning behind this is to  learn selenium  for my work
> and get the list of books I have purchased. Killing two birds with one
> stone
> here. The work focus is to see if selenium   can automate some of the
> testing I have to do and collect useful information from the web page for
> my reports. This part of the goal is in the future. As I need to build my
> python skills up.
> 
>  
> 
> Thus far, I have been successful in logging into Audible and showing the
> library of books. I am able to store the table of books and want to use
> BeautifulSoup to extract the relevant information. Information I will want
> from the table is:
> 
> * Author
> * Title
> * Date purchased
> * Length
> * Is the book in a series (there is a link for this)
> * Link to the page storing the publish details.
> * Download link
> 
> Hopefully this has given you enough information on what I am trying to
> achieve at this stage. AS I learn more about what I am doing, I am adding
> possible extra's tasks. Such as verifying if I have the book already
> download via itunes.
> 
>  
> 
> Learning goals:
> 
> Using the BeautifulSoup  structure that I have extracted from the page
> source for the table. I want to navigate the tree structure. BeautifulSoup
> provides children, siblings and parents methods. This is where I get stuck
> with programming logic. BeautifulSoup does provide find_all method plus
> selectors which I do not want to use for this exercise. As I want to learn
> how to walk a tree starting at the root and visiting each node of the
> tree. 

I think you make your life harder than necessary if you avoid the tools 
provided by the library you are using.

> Then I can look at the attributes for the tag as I go. I believe I
> have to set up a recursive loop or function call. Not sure on how to do
> this. Pseudo code:
> 
>  
> 
> Build table structure
> 
> Start at the root node.
> 
> Check to see if there is any children.
> 
> Pass first child to function.
> 
> Print attributes for tag at this level
> 
> In function, check for any sibling nodes.
> 
> If exist, call function again
> 
> If no siblings, then start at first sibling and get its child.
> 
>  
> 
> This is where I get struck. Each sibling can have children and they can
> have siblings. So how do I ensure I visit each node in the tree?

The problem with your description is that siblings do not matter. Just

- process root
- iterate over its children and call the function recursively with every
  child as the new root.

To make the function more useful you can pass a function instead of hard-
coding what you want to do with the elements. Given

def process_elements(elem, do_stuff):
do_stuff(elem)
for child in elem.children:
process_elements(child, do_stuff)

you can print all elements with

soup = BeautifulSoup(...)
process_elements(soup, print)

and

process_elements(soup, lambda elem: print(elem.name))
will print only the names.

You need a bit of error checking to make it work, though.

But wait -- Python's generators let you rewrite process_elements so that you 
can use it without a callback:

def gen_elements(elem):
yield elem
for child in elem.children:
yield from gen_elements(child)

for elem in gen_elements(soup):
print(elem.name)

Note that 'yield from iterable' is a shortcut for
'for x in iterable: yield x', so there are actually two loops in 
gen_elements().

> Any tips or tricks for this would be grateful. As I could use this in
> other situations.


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


Re: [Tutor] Web scraping using selenium and navigating nested dictionaries / lists.

2019-01-27 Thread mhysnm1964
Marco,

 

Thanks. The reason for learning selenium is for the automation. As I want to 
test web sites for keyboard and mouse interaction and record the results. That 
at least is the long term goal. In the short term, I will have a look at your 
suggestion.

 

 

From: Marco Mistroni  
Sent: Sunday, 27 January 2019 9:46 PM
To: mhysnm1...@gmail.com
Cc: tutor@python.org
Subject: Re: [Tutor] Web scraping using selenium and navigating nested 
dictionaries / lists.

 

Hi my 2 cents. Have a look at scrapy for scraping.selenium is v good  tool to 
learn but is mainly to automate uat of guis

Scrapy will scrape for you and u can automate it via cron. It's same stuff I am 
doing ATM

Hth

On Sun, Jan 27, 2019, 8:34 AM mailto:mhysnm1...@gmail.com>  wrote:

All,



Goal of new project.

I want to scrape all my books from Audible.com that I have purchased.
Eventually I want to export this as a CSV file or maybe Json. I have not got
that far yet. The reasoning behind this is to  learn selenium  for my work
and get the list of books I have purchased. Killing two birds with one stone
here. The work focus is to see if selenium   can automate some of the
testing I have to do and collect useful information from the web page for my
reports. This part of the goal is in the future. As I need to build my
python skills up. 



Thus far, I have been successful in logging into Audible and showing the
library of books. I am able to store the table of books and want to use
BeautifulSoup to extract the relevant information. Information I will want
from the table is:

*   Author 
*   Title
*   Date purchased 
*   Length
*   Is the book in a series (there is a link for this)
*   Link to the page storing the publish details. 
*   Download link

Hopefully this has given you enough information on what I am trying to
achieve at this stage. AS I learn more about what I am doing, I am adding
possible extra's tasks. Such as verifying if I have the book already
download via itunes.



Learning goals:

Using the BeautifulSoup  structure that I have extracted from the page
source for the table. I want to navigate the tree structure. BeautifulSoup
provides children, siblings and parents methods. This is where I get stuck
with programming logic. BeautifulSoup does provide find_all method plus
selectors which I do not want to use for this exercise. As I want to learn
how to walk a tree starting at the root and visiting each node of the tree.
Then I can look at the attributes for the tag as I go. I believe I have to
set up a recursive loop or function call. Not sure on how to do this. Pseudo
code:



Build table structure

Start at the root node.

Check to see if there is any children.

Pass first child to function.

Print attributes for tag at this level 

In function, check for any sibling nodes.

If exist, call function again 

If no siblings, then start at first sibling and get its child.



This is where I get struck. Each sibling can have children and they can have
siblings. So how do I ensure I visit each node in the tree? 

Any tips or tricks for this would be grateful. As I could use this in other
situations.



Sean 

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

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


Re: [Tutor] Web scraping using selenium and navigating nested dictionaries / lists.

2019-01-27 Thread mhysnm1964
Peter,

I am aware that I am avoiding functions that can make my life easier. But I
want to learn some of this data structure navigation concepts to improve my
skills in programming. What you have provided I will review in depth and
have a play with.

A big thanks.


-Original Message-
From: Tutor  On Behalf Of
Peter Otten
Sent: Sunday, 27 January 2019 10:13 PM
To: tutor@python.org
Subject: Re: [Tutor] Web scraping using selenium and navigating nested
dictionaries / lists.

mhysnm1...@gmail.com wrote:

> All,
> 
>  
> 
> Goal of new project.
> 
> I want to scrape all my books from Audible.com that I have purchased.
> Eventually I want to export this as a CSV file or maybe Json. I have 
> not got that far yet. The reasoning behind this is to  learn selenium  
> for my work and get the list of books I have purchased. Killing two 
> birds with one stone
> here. The work focus is to see if selenium   can automate some of the
> testing I have to do and collect useful information from the web page 
> for my reports. This part of the goal is in the future. As I need to 
> build my python skills up.
> 
>  
> 
> Thus far, I have been successful in logging into Audible and showing 
> the library of books. I am able to store the table of books and want 
> to use BeautifulSoup to extract the relevant information. Information 
> I will want from the table is:
> 
> * Author
> * Title
> * Date purchased
> * Length
> * Is the book in a series (there is a link for this)
> * Link to the page storing the publish details.
> * Download link
> 
> Hopefully this has given you enough information on what I am trying to 
> achieve at this stage. AS I learn more about what I am doing, I am 
> adding possible extra's tasks. Such as verifying if I have the book 
> already download via itunes.
> 
>  
> 
> Learning goals:
> 
> Using the BeautifulSoup  structure that I have extracted from the page 
> source for the table. I want to navigate the tree structure. 
> BeautifulSoup provides children, siblings and parents methods. This is 
> where I get stuck with programming logic. BeautifulSoup does provide 
> find_all method plus selectors which I do not want to use for this 
> exercise. As I want to learn how to walk a tree starting at the root 
> and visiting each node of the tree.

I think you make your life harder than necessary if you avoid the tools
provided by the library you are using.

> Then I can look at the attributes for the tag as I go. I believe I 
> have to set up a recursive loop or function call. Not sure on how to 
> do this. Pseudo code:
> 
>  
> 
> Build table structure
> 
> Start at the root node.
> 
> Check to see if there is any children.
> 
> Pass first child to function.
> 
> Print attributes for tag at this level
> 
> In function, check for any sibling nodes.
> 
> If exist, call function again
> 
> If no siblings, then start at first sibling and get its child.
> 
>  
> 
> This is where I get struck. Each sibling can have children and they 
> can have siblings. So how do I ensure I visit each node in the tree?

The problem with your description is that siblings do not matter. Just

- process root
- iterate over its children and call the function recursively with every
  child as the new root.

To make the function more useful you can pass a function instead of hard-
coding what you want to do with the elements. Given

def process_elements(elem, do_stuff):
do_stuff(elem)
for child in elem.children:
process_elements(child, do_stuff)

you can print all elements with

soup = BeautifulSoup(...)
process_elements(soup, print)

and

process_elements(soup, lambda elem: print(elem.name)) will print only the
names.

You need a bit of error checking to make it work, though.

But wait -- Python's generators let you rewrite process_elements so that you
can use it without a callback:

def gen_elements(elem):
yield elem
for child in elem.children:
yield from gen_elements(child)

for elem in gen_elements(soup):
print(elem.name)

Note that 'yield from iterable' is a shortcut for 'for x in iterable: yield
x', so there are actually two loops in gen_elements().

> Any tips or tricks for this would be grateful. As I could use this in 
> other situations.


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

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


Re: [Tutor] python-files

2019-01-27 Thread Asad
Hi All ,

  I tried the following code  :

parser = argparse.ArgumentParser()
parser.add_argument("first")
parser.add_argument("second", nargs="?")
args = parser.parse_args()
print("first:", args.first)

print("second:", args.second)

When I execute the script it gives error :

python python_json_20001_oratest_v1.py "file1"
('first:', 'file1')
('second:', None)
Traceback (most recent call last):
  File "test.py", line 211, in 
with open(args.second, 'r') as f :
TypeError: coercing to Unicode: need string or buffer, NoneType found


if I see in line number 211 it with open(args.second, 'r') as f :

try :
with open(args.second, 'r') as f :
 for line in f:
print line
except IOError:
 print "The default error is err-1 because file2 was not provided "

Does that mean my try and except block is not working because if
args.second  is None as in this case then it should print "The default
error is err-1 because file2 was not provided "

Please advice ,

Thanks,


> -- Forwarded message --
> From: Peter Otten <__pete...@web.de>
> To: tutor@python.org
> Cc:
> Bcc:
> Date: Sun, 27 Jan 2019 10:30:12 +0100
> Subject: Re: [Tutor] python - files
> Cameron Simpson wrote:
>
> > Mats has mentioned the modules getopt and argparse etc. These are
> > primarily aimed at option parsing ("-v", "-o foo"). Your situation
> > occurs _after_ the option parsing (in your case, there are no options).
>
> Not argparse. The main advantage over optparse is its handling of
> positional
> arguments. Your custom logic
>
> >   def main(argv):
> > cmd = argv.pop(0)   # collect the command word
> > badopts = False
> > # mandatory first argument
> > if not argv:
> >   print("%s: missing first argument" % cmd, file=sys.stderr)
> >   badopts = True
> > else:
> >   first = argv.pop(0)
> >   # optional second argument
> >   if argv:
> > second = argv.pop(0)# explicit argument 2, use it
> >   else:
> > second = None   # or some otherdefault
> >   if argv:
> > print("%s: extra arguments: %r" % (cmd, argv), file=sys.stderr)
> > badopts = true
> > if badopts:
> >   print("%s: invalid invocation, aborting" % cmd, file=sys.stderr)
> >   return 2
> > ... work with first and second ...
>
> can roughly be replicated with the two lines
>
> parser.add_argument("first")
> parser.add_argument("second", nargs="?")
>
> A working script:
>
> $ cat test.py
> #!/usr/bin/python3
> import argparse
>
> def main():
> parser = argparse.ArgumentParser()
>
> parser.add_argument("first")
> parser.add_argument("second", nargs="?")
>
> args = parser.parse_args()
>
> print("first:", args.first)
> print("second:", args.second)
>
> if __name__ == "__main__":
> main()
>
> $ ./test.py
> usage: test.py [-h] first [second]
> test.py: error: the following arguments are required: first
>
> $ ./test.py -h
> usage: test.py [-h] first [second]
>
> positional arguments:
>   first
>   second
>
> optional arguments:
>   -h, --help  show this help message and exit
>
> $ ./test.py ONE
> first: ONE
> second: None
>
> $ ./test.py ONE TWO
> first: ONE
> second: TWO
>
> $ ./test.py ONE TWO THREE
> usage: test.py [-h] first [second]
> test.py: error: unrecognized arguments: THREE
>
> Argparse makes a usable standard command line interface easy to set up (if
> you need non-standard behaviour it gets a bit harder).
>
> There is also a companion module argcomplete (not in the stdlib) that
> enables autocompletion.
>
>
>
>
>
>
> -- Forwarded message --
> From: Cameron Simpson 
> To: tutor@python.org
> Cc:
> Bcc:
> Date: Sun, 27 Jan 2019 20:54:13 +1100
> Subject: Re: [Tutor] python - files
> On 27Jan2019 10:30, Peter Otten <__pete...@web.de> wrote:
> >Cameron Simpson wrote:
> >> Mats has mentioned the modules getopt and argparse etc. These are
> >> primarily aimed at option parsing ("-v", "-o foo"). Your situation
> >> occurs _after_ the option parsing (in your case, there are no options).
> >
> >Not argparse. The main advantage over optparse is its handling of
> positional
> >arguments.
>
> I stand corrected.
>
> >Your custom logic
> [...]
> >can roughly be replicated with the two lines
> >
> >parser.add_argument("first")
> >parser.add_argument("second", nargs="?")
> >[... extended example ...]
>
> Thank you!
>
> Cheers,
> Cameron Simpson 
>
>
>
>
> -- Forwarded message --
> From: Marco Mistroni 
> To: mhysnm1...@gmail.com
> Cc: tutor@python.org
> Bcc:
> Date: Sun, 27 Jan 2019 10:46:06 +
> Subject: Re: [Tutor] Web scraping using selenium and navigating nested
> dictionaries / lists.
> Hi my 2 cents. Have a look at scrapy for scraping.selenium is v good  tool
> to learn but is mainly to automate uat of guis
> Scrapy will scrape for you and u can automate it via cron. It's same stuff
> I am doing ATM
> Hth
>
> On Sun, Jan 27, 2019, 8:34 AM 
> > All,
> >
> >
> >
> > 

Re: [Tutor] python-files

2019-01-27 Thread Peter Otten
Asad wrote:

> Hi All ,
> 
>   I tried the following code  :
> 
> parser = argparse.ArgumentParser()
> parser.add_argument("first")
> parser.add_argument("second", nargs="?")
> args = parser.parse_args()
> print("first:", args.first)
> 
> print("second:", args.second)
> 
> When I execute the script it gives error :
> 
> python python_json_20001_oratest_v1.py "file1"
> ('first:', 'file1')
> ('second:', None)
> Traceback (most recent call last):
>   File "test.py", line 211, in 
> with open(args.second, 'r') as f :
> TypeError: coercing to Unicode: need string or buffer, NoneType found
> 
> 
> if I see in line number 211 it with open(args.second, 'r') as f :
> 
> try :
> with open(args.second, 'r') as f :
>  for line in f:
> print line
> except IOError:
>  print "The default error is err-1 because file2 was not provided
>  "

How do you know that the file was not provided? The name might have been 
misspelt or the user lacks the permission to read it.

> Does that mean my try and except block is not working because if
> args.second  is None as in this case then it should print "The default
> error is err-1 because file2 was not provided "
> 
> Please advice ,

If you just want to terminate the script with a helpful message you should 
make the second argument mandatory, too:

parser = argparse.ArgumentParser()
parser.add_argument("first")
parser.add_argument("second")
args = parser.parse_args()

That way the parse_args() call will terminate the script and the user will 
see an error message immediately.

If for some reason you want to keep the argument optional you can check for 
None before trying to open the file:

if args.second is not None:
try:
with open(args.second, 'r') as f :
for line in f:
print line
except IOError as err:
print err  # print actually what happened, not what you guess
else:
print "File 'second' was not provided on the command line"



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


Re: [Tutor] python-files

2019-01-27 Thread Alan Gauld via Tutor
On 27/01/2019 14:57, Asad wrote:

> print("first:", args.first)
> print("second:", args.second)
> 
> When I execute the script it gives error :
> 
> python python_json_20001_oratest_v1.py "file1"
> ('first:', 'file1')
> ('second:', None)

Note that the second file is None.

> Traceback (most recent call last):
>   File "test.py", line 211, in 
> with open(args.second, 'r') as f :
> TypeError: coercing to Unicode: need string or buffer, NoneType found

Note that you get a TypeError because you passed None instead
of a valid filename.

> try :
> with open(args.second, 'r') as f :
>  for line in f:
> print line
> except IOError:
>  print "The default error is err-1 because file2 was not provided "

Note that you are trying to catch an IOError but you are getting a
TypeError. You need to add another except clause for the TypeError.
Or test for a None second file value at the start of your code...

> Does that mean my try and except block is not working because if
> args.second  is None as in this case then it should print "The default
> error is err-1 because file2 was not provided "

It is working. You just aren't catching the correct error type.
It will only type the message you've given if you get an
IOError, but the open() code isn't getting that far,
it's failing on the parameter type.

> Please advice ,

OK, I advise you to delete the text below where you no longer
need it. Some users pay by the byte and all this excess text
costs them money. More than 75% of your message is included
text that we have already seen.

> Thanks,
> 
> 
>> -- Forwarded message --
>> From: Peter Otten <__pete...@web.de>
>> To: tutor@python.org
...

-- 
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