2

ファイルに関する一連の情報を返すこのコードがあります。そして、2 番目のスニペットに示すように、出力を 2 回返します。

list = os.listdir(".")
print Style.BRIGHT + "size        info        last-accessed    last-changed     filename" + Style.RESET_ALL
for f in list:
    info = os.stat(f)
    # "  size          info        last-accessed    last-changed     filename
    print " ", info[ST_SIZE], "                                                  " + f

そして出力:

  size        info        last-accessed    last-changed     filename
  6243                                                   .bash_history
  55                                                   .bash_profile
  3                                                   .CFUserTextEncoding
  15364                                                   .DS_Store
  102                                                   .fontconfig
  105                                                   .sh_history
  102                                                   .ssh
  442                                                   .Trash
  0                                                   .Xauthority
  105                                                   console
  6688                                                   console.py
  272                                                   Desktop
  238                                                   Documents
  442                                                   Downloads
  2924                                                   Folder
  1156                                                   Library
  204                                                   Movies
  102                                                   Music
  136                                                   Pictures
  204                                                   Public
  170                                                   Sites
  3914                                                   xetch
size        info        last-accessed    last-changed     filename
  6243                                                   .bash_history
  55                                                   .bash_profile
  3                                                   .CFUserTextEncoding
  15364                                                   .DS_Store
  102                                                   .fontconfig
  105                                                   .sh_history
  102                                                   .ssh
  442                                                   .Trash
  0                                                   .Xauthority
  105                                                   console
  6688                                                   console.py
  272                                                   Desktop
  238                                                   Documents
  442                                                   Downloads
  2924                                                   Folder
  1156                                                   Library
  204                                                   Movies
  102                                                   Music
  136                                                   Pictures
  204                                                   Public
  170                                                   Sites
  3914                                                   xetch

メッシュで申し訳ありませんが、お気づきのとおり、2 回返されました。

4

1 に答える 1

5

表示するコードは、size info...ヘッダーを 1 回だけ出力します。2 回表示されているので、これはコード ブロック全体が 2 回実行されることを意味します。なぜそうなるのかは、さらにコードを見ないとわかりません。

于 2013-01-03T09:19:11.917 に答える