473,471 Members | 2,062 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Is this a bug or am I crazy?

8 New Member
I have a piece of code as follows :-
Expand|Select|Wrap|Line Numbers
  1.     files = getallfiles(path)
  2.     filteredfiles = [x for x in files if not (x.lower().split('.')[-1] in no_ext)]
  3.     print filteredfiles
  4.     return filteredfiles
  5.  
It may be clumsy code but the intention is to take the list of files from getallfiles()
and exclude those files with the extension in no_ext which is a list of file extensions I don't want. (no_ext = ['pyc', 'zip', 'ini'])

Here's the problem, the above code works fine, but if I remove the
print filteredfiles
command the files with the unwanted extensions are still there in filteredfiles
I even tried putting in
filteredfiles = filteredfiles[:]
but that didn't help.

It looks like the list comprehension is not working until I print out filteredfiles!

I must be doing something wrong....

(python 2.5)
Mar 21 '07 #1
4 986
ghostdog74
511 Recognized Expert Contributor
I have a piece of code as follows :-

files = getallfiles(path)
filteredfiles = [x for x in files if not (x.lower().split('.')[-1] in no_ext)]
print filteredfiles
return filteredfiles

It may be clumsy code but the intention is to take the list of files from getallfiles()
and exclude those files with the extension in no_ext which is a list of file extensions I don't want. (no_ext = ['pyc', 'zip', 'ini'])

Here's the problem, the above code works fine, but if I remove the
print filteredfiles
command the files with the unwanted extensions are still there in filteredfiles
I even tried putting in
filteredfiles = filteredfiles[:]
but that didn't help.

It looks like the list comprehension is not working until I print out filteredfiles!

I must be doing something wrong....

(python 2.5)
can you do this
Expand|Select|Wrap|Line Numbers
  1. ...
  2.     files = getallfiles(path)
  3.     print files , type(files)
  4.     filteredfiles = [x for x in files if not (x.lower().split('.')[-1] in no_ext)]
  5.     print filteredfiles
  6.     return filteredfiles
  7.  
and show the results of the print statements here.thanks
Mar 21 '07 #2
jimscafe
8 New Member
Not sure if this is what you intended. This output shows filteredfiles to be correct (the .pyc files have been removed). It is when I omit the print filteredfiles command that the returned list is incorrect.

Here is the output :-

['c:/programing/source/python/pythonlib\\__init__.py',
'c:/programing/source/python/pythonlib\\datetime\\CalcTime.py',
'c:/programing/source/python/pythonlib\\datetime\\CalcTime.pyc', 'c:/programing/source/python/pythonlib\\datetime\\__init__.py', 'c:/programing/source/python/pythonlib\\files\\FileCopy.py',
'c:/programing/source/python/pythonlib\\files\\FileCopy.pyc',
'c:/programing/source/python/pythonlib\\files\\NewConfigParser.py', 'c:/programing/source/python/pythonlib\\files\\NewConfigParser.pyc', 'c:/programing/source/python/pythonlib\\files\\PollHotFolder.py', 'c:/programing/source/python/pythonlib\\files\\PollHotFolder.pyc',
'c:/programing/source/python/pythonlib\\files\\__init__.py',
'c:/programing/source/python/pythonlib\\files\\__init__.pyc',
'c:/programing/source/python/pythonlib\\messaging\\basicmsg.py', 'c:/programing/source/python/pythonlib\\messaging\\basicmsg.pyc', 'c:/programing/source/python/pythonlib\\messaging\\messageframe.py', 'c:/programing/source/python/pythonlib\\messaging\\messageframe.pyc',
'c:/programing/source/python/pythonlib\\messaging\\textfileprotocol.py',
'c:/programing/source/python/pythonlib\\messaging\\textfileprotocol.pyc',
'c:/programing/source/python/pythonlib\\messaging\\__init__.py',
'c:/programing/source/python/pythonlib\\messaging\\__init__.pyc']
<type 'list'>
['c:/programing/source/python/pythonlib\\__init__.py',
'c:/programing/source/python/pythonlib\\datetime\\CalcTime.py',
'c:/programing/source/python/pythonlib\\datetime\\__init__.py', 'c:/programing/source/python/pythonlib\\files\\FileCopy.py', 'c:/programing/source/python/pythonlib\\files\\NewConfigParser.py',
'c:/programing/source/python/pythonlib\\files\\PollHotFolder.py',
'c:/programing/source/python/pythonlib\\files\\__init__.py',
'c:/programing/source/python/pythonlib\\messaging\\basicmsg.py', 'c:/programing/source/python/pythonlib\\messaging\\messageframe.py', 'c:/programing/source/python/pythonlib\\messaging\\textfileprotocol.py', 'c:/programing/source/python/pythonlib\\messaging\\__init__.py']
Mar 21 '07 #3
bartonc
6,596 Recognized Expert Expert
I have a piece of code as follows :-
Expand|Select|Wrap|Line Numbers
  1.     files = getallfiles(path)
  2.     filteredfiles = [x for x in files if not (x.lower().split('.')[-1] in no_ext)]
  3.     print filteredfiles
  4.     return filteredfiles
  5.  
It may be clumsy code but the intention is to take the list of files from getallfiles()
and exclude those files with the extension in no_ext which is a list of file extensions I don't want. (no_ext = ['pyc', 'zip', 'ini'])

Here's the problem, the above code works fine, but if I remove the
print filteredfiles
command the files with the unwanted extensions are still there in filteredfiles
I even tried putting in
filteredfiles = filteredfiles[:]
but that didn't help.

It looks like the list comprehension is not working until I print out filteredfiles!

I must be doing something wrong....

(python 2.5)
Hello (is it Jim?). If you provide the entire code (including getallfiles()) then we'll have a chance to run it on different versions of python and see if we can track this down for you. Please read "REPLY GUIDELINES" on the right hand side of the page (while composing your reply) to see what CODE tags look like. Thanks for joining.
Mar 21 '07 #4
jimscafe
8 New Member
Thanks guys, there's no fool like an old fool.

The problem was some debug print statements that made it look like the list had not been changed...but in fact it had been changed.

All is well now and no need to tell the python programmers to re-write python!!!!
Mar 21 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: dont bother | last post by:
This is really driving me crazy. I have a dictionary feature_vectors{}. I try to sort its keys using #apply sorting on feature_vectors sorted_feature_vector=feature_vectors.keys()...
9
by: Leif K-Brooks | last post by:
I try to make my code comply to the Python style guide (http://www.python.org/peps/pep-0008.html). Last time I read it, I swear that it said to use CamelCase for often-used functions and...
1
by: Bulba! | last post by:
OK. I have reworked this program (below) to use just the data manipulation capabilities of Python (training was largely the motivation). I've tried to manipulate the data just in Python and not in...
7
by: Oliver Andrich | last post by:
Hi everybody, I have to write a little skript, that reads some nasty xml formated files. "Nasty xml formated" means, we have a xml like syntax, no dtd, use html entities without declaration and...
11
by: doltharz | last post by:
Please Help me i'm doing something i though was to be REALLY EASY but it drives me crazy The complete code is at the end of the email (i mean newsgroup article), i always use Option...
19
by: Daniel Billingsley | last post by:
I think it's really nice to be able to do code like someVariable = someMethod(new SomeClass("some text")); However, as method signatures are number and types of parameters, you're limited to...
3
by: squash | last post by:
I have spent two hours trying to make sense of this script, called crazy.php. The output should be nothing because $cookie_password is nowhere defined in this script, correct? But it actually...
28
by: onkar | last post by:
This idea might be vey crazy. But I hope to get answers to this .. from comp.lang.c If a compiler is designed such that it automatically adds a free() matching every malloc() then is it not a...
0
by: eighthman11 | last post by:
Access 2000. Sql Server I know this is going to sound crazy but I have a continuous form which is bound to a sql table with an ODBC connection. I filter the records in the Continous form...
3
by: rashpal.sidhu | last post by:
Please help, this problem is driving me crazy !! I am using metaphone to create phonetic keys. When i run the module stand-a-lone it works fine. I'm trying to create a runner for informix...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.