亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

Chinaunix

標題: 輸出文件指定行失效了(已經(jīng)解決) [打印本頁]

作者: laohuanggua    時間: 2012-09-25 10:37
標題: 輸出文件指定行失效了(已經(jīng)解決)
本帖最后由 laohuanggua 于 2012-09-25 11:20 編輯

數(shù)據(jù)文件內(nèi)容。有5行。其中第2行是空的。
  1. # cat /opt/stuff/test.txt
  2. First let's print the whole file:

  3. To all the people out there.
  4. I say I don't like my hair.
  5. I need to shave it off.
復制代碼
程序代碼
  1. #!/usr/bin/python
  2. #scriptname:/opt/learnpython/19.py

  3. script,input_file=argv
  4. def print_all(datafilename):
  5.         print datafilename.read()

  6.        
  7. def rewind(datafilename):
  8.         datafilename.seek(0)

  9. def print_a_line(line_number,datafilename):
  10.         print line_number,datafilename.readline()

  11. current_file=open(datafilename)

  12. print_all(current_file)

  13. rewind(current_file)

  14. print_a_line(1,current_file)

  15. print_a_line(1,current_file)
  16. #END
復制代碼
# python /opt/learnpython/19.py /opt/stuff/test.txt
  1. First let's print the whole file:

  2. To all the people out there.
  3. I say I don't like my hair.
  4. I need to shave it off.

  5. 1 First let's print the whole file:

  6. 1
復制代碼
注意:最后一個是空的。。

作者: laohuanggua    時間: 2012-09-25 11:11
問題解決了。第二次print_a_line的時候,順序執(zhí)行到第2行。所以必然是空的。
作者: laohuanggua    時間: 2012-09-25 11:19
本帖最后由 laohuanggua 于 2012-09-25 11:28 編輯

順便說一個關于導入module的事情

我之前用
#from sys import argv
用argv就可以。

但是如果改成
import sys
則立刻報錯
NameError: name 'argv' is not defined


但是
>>> import sys
>>> dir(sys)
['__displayhook__', '__doc__', '__excepthook__', '__name__', '__package__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_getframe', 'api_version', 'argv', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dont_write_bytecode', 'exc_clear', 'exc_info', 'exc_type', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'getcheckinterval', 'getdefaultencoding', 'getdlopenflags', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'gettrace', 'hexversion', 'last_traceback', 'last_type', 'last_value', 'maxint', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1', 'ps2', 'py3kwarning', 'setcheckinterval', 'setdlopenflags', 'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 'subversion', 'version', 'version_info', 'warnoptions']
>>>



搞定了。
這樣就可以

import sys

script,datafilename=sys.argv

作者: linux_c_py_php    時間: 2012-09-25 12:18
汗, 讀一次偏移量就后移了, 當然是繼續(xù)讀第二行了.

laohuanggua 發(fā)表于 2012-09-25 11:11
問題解決了。第二次print_a_line的時候,順序執(zhí)行到第2行。所以必然是空的。

作者: linux_c_py_php    時間: 2012-09-25 12:19
汗, 引入模塊和引入模塊內(nèi)的名稱是兩碼事,
  1. from sys import argv
  2. #這里就可以直接寫argv

  3. import sys
  4. #這里必須寫sys.argv
復制代碼
laohuanggua 發(fā)表于 2012-09-25 11:19
順便說一個關于導入module的事情

我之前用

作者: laohuanggua    時間: 2012-09-26 09:39
linux_c_py_php 發(fā)表于 2012-09-25 12:18
汗, 讀一次偏移量就后移了, 當然是繼續(xù)讀第二行了.


我以為必須有一個指針下移的操作呢。




歡迎光臨 Chinaunix (http://72891.cn/) Powered by Discuz! X3.2