浙江省高等学校教师教育理论培训

微信搜索“毛凌志岗前心得”小程序

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
create=twf.TableForm('site_create',action='/sites/create.html',children=[
    twf.TextField("show_name",label_text="chinese site name:"),
    twf.TextField("name",label_text="english short site name:"),
    twf.TextField("url"),
    twf.SingleSelectField("encode",options=['gb2312', 'utf-8'],label_text="page encode"),
    twf.TextField("path",attrs={"value":"/home/mlzboy"},label_text="store root path"),
    twf.TextArea("memo"),
    ])   
  •  
      Note 5: We have not specified the full path to the file in the code above so the file will be treated as being in the current folder. However we can pass a full path name to the file() or open() functions instead of just the file name. There is a wrinkle when using Windows however, because the \ character used to separate folders in a Windows path has a special meaning inside a Python string so when specifying paths in Python it is best to use the / character instead and that will work on any Operating System including Windows.
       
    •  
      1. A Carriage Return (CR) character ('\r')
      2. A Line Feed (LF) character ('\n')
      3. A CR/LF pair ('\r\n').
    • Some Operating Systems Gotchas


      Operating systems handle files in different ways. This introduces some niggles into our programs if we want them to work on multiple operating systems. There are two niggles in particular which can catch people out and we'll look at them here:

      Newlines


      The whole subject of newlines and text files is a murky area of non standard implementation by different operating systems. These differences have their roots in the early days of data communications and the control of mechanical teleprinters. Basically there are 3 different ways to indicate a new line:

      All three techniques are used in different operating systems. MS DOS (and therefore Windows) uses method 3. Unix (including Linux) uses method 2. Apple in its original MacOS used method 1, but now uses method 2 since MacOS X is really a variant of Unix.


      So how can the poor programmer cope with this multiplicity of line endings? In many languages she just has to do lots of tests and take different action per OS. In more modern languages, including Python, the language provides facilities for dealing with the mess for you. In the case of Python the assistance comes in the form of the os module which defines a variable called linesep which is set to whatever the newline character is on the current operating system. This makes adding newlines easy, and rstrip() takes account of the OS when it does its work of removing them, so really the simple way to stay sane, so far as newlines are concerned is: always use rstrip() to remove newlines from lines read from a file and always add os.linesep to strings being written to a file.


      That still leaves the awkward situation where a file is created on one OS and then processed on another, incompatible, OS and sadly, there isn't much we can do about that except to compare the end of the line with os.linesep to determine what the difference is.

      Specifying Paths

      This is more of an issue for Windows users than others although MacOS 9 users may bump into it occasionally too. As above each OS specifies paths to files using different characters to separate the drives, folders and files. The generic solution for this is again to use the os module which provides the os.sep variable to define the current platforms path separator character. In practice you won't need to use this very often since the path will likely be different for every machine anyway! So instead you will just enter the full path directly in a string, possibly once for each OS you are running on. But there is one big gotcha hiding in wait for Windows users...

      You saw in the previous section that python treats the string '\n' as a newline character. That is it takes two characters and treats them as one. In fact there are a whole range of these special sequences beginning with back slash (\) including:

       

      more pls visit:

      http://www.freenetpages.co.uk/hp/alan.gauld/tutfiles.htm 

       

      stdin article ,very very good,

      http://stackoverflow.com/questions/2264991/how-to-read-from-stdin-or-from-a-file-if-no-data-is-piped-in-python

      For unix/linux you can detect whether data is being piped in by looking at os.isatty(0)

      $ date | python -c "import os;print os.isatty(0)"
      False
      $ python
      -c "import os;print os.isatty(0)"
      True

      @Ignacio Vazquez-Abrams: for now my workflow is: istty? no: fetch stdin / yes: try opening a file. try ok? carry on / except: display erro message and exit – e-satis Feb 15 at 10:16 

      os.isatty(fd)

      Return True if the file descriptor fd is open and connected to a tty(-like) device, else False.

      Availability: Unix.

      mlzboy@mlzboy-mac:~/my/ide/demo$ python batch_get_list_html.py < list_links.txt 

      完成初始化

      load from stdin

      @11:20:22, {batch_get_html} start

      执行批量下载网页工作

      cat ./list_links.txt|python batch_get_list_html.py 

       

      http://docs.python.org/library/fileinput.html

      svc

       

      http://cr.yp.to/daemontools/svc.html

       

      布隆过滤器(bloom filter)介绍与python版的实现
      2009-06-25 08:45
      python fileinput module

       释译python fileinput中的description

      refercence:

  • 使用global语句

    如果你想要为一个定义在函数外的变量赋值,那么你就得告诉Python这个变量名不是局部的,而是全局的。我们使用global语句完成这一功能。没有global语句,是不可能为定义在函数外的变量赋值的。

     

    Note 2: We used the file() function to open the file, older versions of Python used the built in function open() instead. The parameters are identical but since open() is still the preferred mechanism we will usually use open() however, if you find file() more logical then feel free to use that instead.

     

posted on 2010-09-21 15:26  lexus  阅读(243)  评论(0编辑  收藏  举报