简体   繁体   English

python程序的输出不一致

[英]Inconsistent output from python program

This question is related to my earlier post:-这个问题与我之前的帖子有关:-

I ran this program in the IDLE interpreter:我在 IDLE 解释器中运行了这个程序:

      import os
      def rename_files():
            file_list = os.listdir("C:\prank")
            print (file_list)

Sometimes, this program executes as intended, ie the list of filenames specified in the argument gets printed in the Python shell.有时,该程序会按预期执行,即参数中指定的文件名列表会打印在 Python shell 中。 but other times, the shell simply restarts without any other output, the code itself seems fine.但其他时候,shell 只是在没有任何其他输出的情况下重新启动,代码本身似乎很好。 I tried prefixing the argument with 'r' so that the program accepts the file path 'as is'.我尝试在参数前加上“r”,以便程序“按原样”接受文件路径。 Same result.结果一样。 I have tried changing directories, with the same result.我尝试更改目录,结果相同。

What else should I try?我还应该尝试什么? Thank you in advance.先感谢您。

You need to call the function you define:您需要调用您定义的函数:

import os

def rename_files(path):
     file_list = os.listdir(path)
     print (file_list)


rename_files(r"C:\prank")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM