473,387 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Change to previous directory with glob

I have a script where I am using glob to get a list of folders that end in "*import", but I am trying to change my directory to the previous one.

Here is what I have:

@directories = glob("*import");
@directories = sort { $b <=> $a } @files;

I can't figure out how to make glob go back one directory. I am looking for something similiar to the Unix "cd .." command.

I have tried glob("../*import") and dozens of variations, but nothing works.

Thanks
Aug 26 '07 #1
4 2086
miller
1,089 Expert 1GB
How I would do it:

Expand|Select|Wrap|Line Numbers
  1. use File::Spec::Functions qw(updir);
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my @files = do {
  7.     local *PARENTDIR;
  8.     opendir(PARENTDIR, updir) or die "Can't open dir: $!";
  9.     grep {/import$/} readdir(PARENTDIR);
  10. };
  11.  
  12. print "@files";
  13.  
Alternatively, just use chdir.

- Miller
Aug 26 '07 #2
I am an idiot.

I was using this code (that I had used earlier):

@files = glob("*import");
@files = sort { $b <=> $a } @files;

when I decided to change the word "files" to directory - and so I did this:

@directory = glob("*import");
@directory = sort { $b <=> $a } @files;

As you can see, I did not change the third "@files" on the sort line. Once I changed it to "@directory", it worked.

I guess that I should quit programming at 2:00 a.m. - after that, I am not thinking clearly....

Thanks for your help.
Aug 26 '07 #3
numberwhun
3,509 Expert Mod 2GB
Nah, I don't think you should quit the 2am suicide programming session(s), in fact I tend to get some really good work done during those weeeee morning hours. Instead, I would crank up the ol' tea pot and start steeping some tea (or whatever your beverage of choice is). Although the only issues I have truly had during those times are when I wake up with drool on the keyboard and an imprint of the keys on my cheek. Yeah, then you know you were up too long!

Glad you found your issue!

Regards,

Jeff
Aug 26 '07 #4
miller
1,089 Expert 1GB
As you can see, I did not change the third "@files" on the sort line. Once I changed it to "@directory", it worked.
I don't know for sure, but I suspect that you are probably not including "use strict;" in your scripts. Or at least not this script. If you had, then once you changed the original my definition from "my @files" to "my @directory", all other instances of @files would no longer work and you would have been alerted to the error.

This is just a suspicion. But if it's true, then I strongly encourage you to change your script to include "use strict;" at the top. It would have saved you this headache, and will definitely save you future headaches.

- Miller
Aug 26 '07 #5

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

Similar topics

3
by: Sara Fwd | last post by:
Can you guys also help me find a module that looks in a directory and print out the names of the files in there? __________________________________ Do you Yahoo!? Yahoo! Mail - Helps...
6
by: Gonnasi | last post by:
With >glob.glob("*") or >os.listdir(cwd) I can get a combined file list with directory list, but I just wanna a bare file list, no directory list. How to get it? Tons of thanks in advance!
15
by: Jameson | last post by:
Happy New Year, Everyone! I am trying to figure out how to display a bunch of images (mainly JPEGs, but possibly a few GIFs and PNGs as well) that are stored in a local directory on the system....
11
by: comp.lang.php | last post by:
Once again, I thought my class method deleteZip() would do the trick, but it never deletes any .zip* file found in a directory: /** * Delete any latent ZIP files found in this album. This...
3
by: RAMohrmann | last post by:
Greetings, I am attempting to view all files in a directory and if those files have not been modified within the last couple days I will remove them. In order to do this I need to look at the...
2
by: Kevin Walzer | last post by:
I am trying to create a number of Tk PhotoImages from a single directory. Currently I am hard-coding file names and image names, like so: def makeImages(self): self.imagedir = (os.getcwd() +...
9
by: Wolfgang Draxinger | last post by:
H folks, I got, hmm not really a problem, more a question of elegance: In a current project I have to read in some files in a given directory in chronological order, so that I can concatenate...
1
by: jo3c | last post by:
hi everybody im a newbie in python, i have a question how do u parse a bunch of text files in a directory? directory: /dir files: H20080101.txt ,...
10
by: royG | last post by:
hi i wrote a function to parse a given directory and make a sorted list of files with .txt,.doc extensions .it works,but i want to know if it is too bloated..can this be rewritten in more...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.