---------------------------------------
Import
Sys
...
If__
Name__= '_ Main __':
Oldstdout =
Sys.
Stdout
Sys.
Stdout=
Open("Script. log", "W + ")
PrintScript. schoolsong (download ())
Sys.
Stdout= Oldstdout
----------------------------------------
Use PDB for debugging.
Python comes with a debugger called PDB, which is similar to gnu gbd. The following uses a simple program to demonstrate the functions of PDB. The program code is as follows:
#!/usr/bin/python
import pdb
a = "aaa"
pdb.set_trace()
b = "bbb"
c = "ccc"
final = a + b + c
print final
The program has imported the PDB module and added the PDB. set_trace () tracking point in the code. Now let's run the program.
Localhost :~ /Python/PDB # Python pdbtest. py
-- Return --
>/Usr/lib/python2.3/PDB. py (992) set_trace ()-> none
-> PDB (). set_trace () # execution starts from the tracking point
(PDB) N # N read the next line of code
>/Root/Python/PDB/pdbtest. py (6 )? ()
-> B = "BBB"
(PDB) N
>/Root/Python/PDB/pdbtest. py (7 )? ()
-> C = "CCC"
(PDB) p B # P print the variable value
'Bbb'
(PDB) L # l display the current execution position
2
3 Import PDB
4 A = "AAA"
5 PDB. set_trace ()
6 B = "BBB"
7-> C = "CCC"
8 Final = A + B + C
9 print final
10
[EOF]
(PDB) N
>/Root/Python/PDB/pdbtest. py (8 )? ()
-> Final = A + B + C
(PDB) N # If the command is the same as the previous one, press enter without entering 'n'
>/Root/Python/PDB/pdbtest. py (9 )? ()
-> Print final
(PDB) N
Aaabbbccc
-- Return --
>/Root/Python/PDB/pdbtest. py (9 )? ()-> None
-> Print final
(PDB) p a, B, C, final
('Aaa', 'bbb ', 'ccc', 'aaabbbccc ')
(PDB)
('Aaa', 'bbb ', 'ccc', 'aaabbbccc ')
(PDB) N
Localhost :~ /Python/PDB # Return Shell
There are many other PDB commands. You can use the help command to list all the PDB commands, and use help p to query the description of the p command.