0

I have a Python 2.7 script that works perfectly only on IDLE, the result looking like this :

**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
>>>** 

But, in cmd I got the result like this :

**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**

I need a result like in the IDLE view. Did somebody ever get the same problem like me? and how to fix it please? and yes I'm in a condition that makes me use only Python 2.7.

Here's my code, the idea is to count *.png file per month in a year

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)))

Thank you in advance

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 1
    Show your code. If it's a lot of code, narrow it down to the minimum code that executes differently. If you read from any files, the difference is likely that file access is assuming different working directories. – Bennett Brown Jul 24 '18 at 03:26
  • Check your working directory. That may be what causing problem. – Mia Jul 24 '18 at 03:28
  • First share your code then only anyone of us can help you properly – abhi krishnan Jul 24 '18 at 03:31
  • " https://drive.google.com/open?id=1AJfASFyL9SSE82-Y0rrQi9a956NIXdIc " here's my code. And I have checked my working directory multiple times. the idea is to count .png file per month in a year – Yuvita Dian Safitri Jul 24 '18 at 03:39
  • 1
    The matcher is almost definitely dependent on the working directory. And you should include the code in your question so that it is available for other readers that may have a similar problem in the future. – Will Richardson Jul 24 '18 at 04:14
  • Please add your code as text, not a link – OneCricketeer Jul 24 '18 at 04:18
  • `os.walk(".")` will look at the current folder, which is `C:\Users\DELL` when you run it from cmd, but will probably be `D:\Act\Temp\2017\Bukti_PPR`. You can do `print(os.getcwd())` to check what the current directory is to debug this. – Will Richardson Jul 24 '18 at 04:27
  • Please **do not** use any hosting service such as Google Drive to share code or resources. Instead put the code in the actual question. – Max von Hippel Jul 24 '18 at 04:27
  • Thank you for all of you, especially @WillRichardson for `print(os.getcwd())` suggestions, that really open my mind. Stuck for a while ago, because the code was working well in Python 3.6, but not in 2.7, I'll post the answer later – Yuvita Dian Safitri Jul 24 '18 at 05:09

1 Answers1

0

Thank You for the suggestions, very helpful

So I add print(os.getcwd()) to my code and really got wrong working directory. Sorry for being stubborn for that, because I have no problem before with Python 3.6

then I do this to cmd, a bit troublesome because I must go to my exact directory, but it's Ok

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

That's the answer, hopefully useful for similar problem