I have been using os.walk() to traverse a bunch of subdirectories inside a directory dir. These subdirectories are numbered from 0001 to 0899. I assumed that os.walk(dir) traverses these subdirectories in numerical order, i.e., as they are shown in the finder (I am on Mac), and so far I have had no reason to believe that this is not true.
However, a few days ago I noticed something strange: os.walk() suddenly (?) traverses the folders non-numerically (but always in the same sequence, I think). I am fairly sure that this was not the case before - I would have noticed.
I am aware that I can use sorted(os.walk(dir)) to have the subdirectories processed numerically, but that does not answer my question. How is it possible that the behaviour of os.walk() changed? Could it have to do with upgrading Python in the meantime (which I don't think I did - can this be checked somehow)?
EDIT: it occurred to my that I updated from OS Sierra to OS Sierra High in the meantime. Maybe that is where the answer lies?
I will answer my own question: it turns out that it was the fact that I had moved dir and its complete contents to a different place on my disk, thus giving them different addresses, that caused the different sorting.
Related
I've been working on a mac app for a long time now, under the assumption that everyone has their Application Support folder located at /Users/[user_name]/Library/Application Support. However, I recently learned this is not true. I need something I can do in python to get the path to the Application Support directory on anyone's computer. Everything I've seen so far has been very outdated with respect to python. The only leads I have are some PyPi packages, PyCocoa and pyobjc-framework-Cocoa. I intend for this to work across several recent OS versions (one person has Monterey, one has Catalina, one has Big Sur, I have Mojave), and it seems that the path to the Application Support folder has changed across versions. What is the accepted way to find this folder on any computer?
Thanks!
Might be a bit slow, but the os library has a walk function which may help. You could loop through the most likely places i.e., $HOME, etc. Then read through the subfolders...something like this should identify the directories - obviously, you will need to check them against your expectation.
[x[0] for x in os.walk(top-directory)]
I created a Perl script reading information from an XLSX sheet. Since on one machine it worked well, and on another it did not, I included a short debug section:
$sheetdate = ($sheet -> {Cells} [0] [$sheet->{MaxCol}]) -> value();
print "value: $sheetdate\n";
$sheetdate = ($sheet -> {Cells} [0] [$sheet->{MaxCol}]) -> get_format();
print "getformat: $sheetdate\n";
On one machine it printed:
value: 2016-01-18
getformat: yyyy-mm-dd
While on the other:
value: 1-18-16
getformat: m-d-yy
Same script, same worksheet, different results. I believe that something in the environment makes the difference, but I do not know what exactly.
Any hints?
"Same script, same worksheet, different results. I believe that something in the environment makes the difference, but I don not know what exactly."
You sort-of indicate here yourself that you're not really seeking the solution to a perl or XLSX problem so much as some assistance with troubleshooting your environment.
Without access to the environment its difficult to offer a solution per se, but I can say this - you need to;
1) Re-arrange things so that you do get the same result from both environments;
2) Identify a list of differences between the original, problem environment and the one that now "works"; and
3) Modify one thing on the list at a time - moving towards the environment that works - checking each time until it becomes clear what the key variable (not in a programming sense) is.
With regards to (1), take a look at Strawberry Perl. Using Strawberry, its relatively easy to set up what some call Perl on a stick (see Portable ZIP edition) - a complete perl environment on a USB stick. Put your document on the same USB and then try the two environments - this time with absolute certainty of having the same environment. If different results persist, try booting from a "live environement" DVD (linux or widows as appropriate), and then using the USB.
Ultimately, I'd suggest there's something (such as a spreadsheet template ) at play that is different between the environments. You just need to go through a process of elimination to find out what it is.
With the benefit of hindsight, I think its worth revisiting this to produce a succinct answer for those who come across this problem in the future.
The original question was how could a perl script produce two different results when the excel data file fed into it is identical (which was confirmed with MD5 checksums). As programmers, our focus tends to be on the scripts we write and the data that goes into them. What slips to the back of the mind is the myriad of ways that perl itself can be installed and configured.
The three things that should assist in determining where the difference between two installs lie are;
(1) Use strawberry perl on a stick as described above to take the environment out of the equation and thereby (if the problem "disappears") confirm that the problem is something to do with the environment.
(2) Use Data::Dumper liberally throughout to find where the flow of execution "forks."
(3) Compare the output of perl -V (note capital V) to find out if there are differences in how the respective perls were built and configured.
The root cause of the problem was an outdated Spreadsheet::XLSX cpan module installed as RPM from the distribution repository. The RPM included version 0.13 of this module, in CPAN there was already version 0.15, and since version 0.14 the module's behaviour was changed in this particular respect. So once I replaced the pre-compiled module with the version downloaded directly from CPAN and compiled locally, the problem was solved.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Does readdir() guarantee an order?
I'm guessing this isn't the case, and I'd need to manually check the name of each entry instead of just skipping the first couple. Is that correct?
The POSIX standard does not guarantee anything about the order of directory entries whatsoever. As such, if you're interested in filtering out . and .., you do need to compare for them.
No, you should never rely on finding . and .. first (although it almost always happens).
I remember one case when I had problems with such an assuption (on ReiserFS they were not found first, but it was an old version of ReiserFS, now it may be different).
It is not in the standard, but I have never seen them in any other place, but the first two. But, just in case, if you don't realy mind the time that much, I would do the check.
I am facing with the bug following:
https://bugzilla.samba.org/show_bug.cgi?id=4531
rsync will always get the older symlink of the other side overwrite
the newer one on the local side.
Wayne has suggested to use unison, however it is a non-developing old
project that I have suspect to use.
What can you suggest me for ?
My main aim is to syncronize file, directories, links for 2 nodes.
unison is ok, as long as your file/folders name don't use unicode, especially cross platform. Can't hurt to give it a try.
See Here for the limitation on unicode in filename.
What is the recommended way of having several cabal packages in one directory?
Why: I have an old project with many separable modules. Since originally they formed just one program it was, and still is, handy to have them in same directory for easy compiling.
Options
Just suffer and split everything, including VCS holding the stuff, into different directories?
Hack cabal until it is happy with multiple .cabal files in same directory?
Make another subdirectory for each module and put .cabal files there along with symlinks to original pieces of code?
Something smarter? What?
I'd have to recommend option 1 or 3 for cleanliness. I'm not sure how to get around this, if there is even a way to get around this.
I'd say a modified option of 1: subdirectories for everything, no symlinks, but keep everything under a single VCS.
This problem is on the issue list for Cabal 2.
I would recommend that this is exactly what workspaces in Leksah were designed to do. Just get your hands on Leksah and then the rest will sort itself out.