在cmd和IDLE python中的结果不同

时间:2018-07-24 03:24:06

标签: python python-2.7 python-idle python-cmd

我有一个Python 2.7脚本,该脚本仅在IDLE上可完美运行,结果如下所示:

**Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec  5 2015, 20:32:19) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> 
================ RESTART: D:\Act\Temp\2017\Bukti_PPR\xxx1.py ================
38, 23, 35, 36, 38, 36, 35, 36, 37, 36, 34, 36, 420
>>>** 

但是,在cmd中,我得到了这样的结果:

**C:\Users\DELL>C:\Python27\python.exe D:\Act\Temp\2017\Bukti_PPR\xxx1.py

0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0**

我需要一个类似IDLE视图的结果。有人遇到过像我一样的问题吗?以及如何解决?是的,我的状况只能使我使用Python 2.7。

这是我的代码,这个想法是每年计算每月* .png文件

import fnmatch
import os

pattern1 = "*201701*.png"
pattern2 = "*201702*.png"
pattern3 = "*201703*.png"
pattern4 = "*201704*.png"
pattern5 = "*201705*.png"
pattern6 = "*201706*.png"
pattern7 = "*201707*.png"
pattern8 = "*201708*.png"
pattern9 = "*201709*.png"
pattern10 = "*201710*.png"
pattern11 = "*201711*.png"
pattern12 = "*201712*.png"
this1 = []
this2 = []
this3 = []
this4 = []
this5 = []
this6 = []
this7 = []
this8 = []
this9 = []
this10 = []
this11 = []
this12 = []
#fold = []
for root, dirs, files in os.walk("."):
    for entry in fnmatch.filter(files, pattern1):
            this1.append(entry)
            #fold.append(os.path.join(root,entry))
    for entry in fnmatch.filter(files, pattern2):
            this2.append(entry)
            #fold.append(os.path.join(root,entry))
    for entry in fnmatch.filter(files, pattern3):
            this3.append(entry)
            #fold.append(os.path.join(root,entry))
    for entry in fnmatch.filter(files, pattern4):
            this4.append(entry)
            #fold.append(os.path.join(root,entry))
    for entry in fnmatch.filter(files, pattern5):
            this5.append(entry)
            #fold.append(os.path.join(root,entry))
    for entry in fnmatch.filter(files, pattern6):
            this6.append(entry)
            #fold.append(os.path.join(root,entry))
    for entry in fnmatch.filter(files, pattern7):
            this7.append(entry)
            #fold.append(os.path.join(root,entry))
    for entry in fnmatch.filter(files, pattern8):
            this8.append(entry)
            #fold.append(os.path.join(root,entry))
    for entry in fnmatch.filter(files, pattern9):
            this9.append(entry)
            #fold.append(os.path.join(root,entry))
    for entry in fnmatch.filter(files, pattern10):
            this10.append(entry)
            #fold.append(os.path.join(root,entry))
    for entry in fnmatch.filter(files, pattern11):
            this11.append(entry)
            #fold.append(os.path.join(root,entry))
    for entry in fnmatch.filter(files, pattern12):
            this12.append(entry)
            #fold.append(os.path.join(root,entry))

bulan = [len(this1),len(this2),len(this3),len(this4),len(this5),len(this6),len(this7),len(this8),len(this9),len(this10),len(this11),len(this12)]
setahun = sum(bulan)
jumlah = [len(this1),len(this2),len(this3),len(this4),len(this5),len(this6),len(this7),len(this8),len(this9),len(this10),len(this11),len(this12), setahun]
#print (jumlah)
print (', '.join(map(str, jumlah)))

提前谢谢

1 个答案:

答案 0 :(得分:0)

谢谢您的建议,非常有帮助

因此,我在代码中添加了print(os.getcwd()),并确实得到了错误的工作目录。抱歉,我很固执,因为我以前对Python 3.6没问题

然后我对cmd执行此操作,有点麻烦,因为我必须转到确切的目录,但是没关系

C:\Users\DELL>d:

D:\>cd Act\Temp\2017\Bukti_PPR

D:\Act\Temp\2017\Bukti_PPR>C:\Python27\python.exe D:\Act\Temp\2017\Bukti_PPR\xxx1.py
D:\Act\Temp\2017\Bukti_PPR
38, 23, 35, 36, 38, 36, 35, 36, 37, 36, 34, 36, 420

这就是答案,希望对类似问题有用