Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Listing of files using glob

by AG87 (Acolyte)
on Jan 05, 2012 at 07:25 UTC ( #946333=perlquestion: print w/ replies, xml ) Need Help??
AG87 has asked for the wisdom of the Perl Monks concerning the following question:

Hello I am basically trying to list all the files present in a given directory that ends with ".fasta" extension. But somehow I am not getting the whole file name....instead just getting the extension ".fasta", thus omitting the name of file before ".fasta" extension. However when I myself create a new file manually, say "a.fasta", it is being printed completely. Can anyone suggest me why the file created via perl code is not being grabbed by the glob function?? My code is

while(my $key = each %seqInfo) { my $nameWithoutFastaSign = $key; $nameWithoutFastaSign =~ tr/>//d; my $currentDir = getcwd; chdir "$currentDir/$nameWithoutFastaSign"; my $changedDir = getcwd; print "changed directory is: $changedDir\n"; while($fastaFile = glob("*.fasta")) { print "$fastaFile\n"; } chdir "$currentDir"; }

and the output shown is

changed directory is: /home/ammarah/Documents/code/1elwA .fasta changed directory is: /home/ammarah/Documents/code/1ghwA .fasta changed directory is: /home/ammarah/Documents/code/1flwA .fasta

There should be a complete file name, which is similar to folder name eg "1elwA.fasta" instead of simple ".fasta". Can anyone help me please??

Comment on Listing of files using glob
Select or Download Code
Re: Listing of files using glob
by Anonymous Monk on Jan 05, 2012 at 07:58 UTC

    Can anyone suggest me why the file created via perl code is not being grabbed by the glob function??

    *ahem* assumes facts not in evidence

    the program you posted is not complete

    the program you posted doesn't use strict or warnings

    the program you posted doesn't create any files or directories

    the program you posted doesn't doesn't check for errors

    chdir can fail, you can check for errors with autodie

      Here is the complete code. It basically takes a file...splits it and save the portions of file in separate files by creating a directory. Afterwards I want to list the individual file using glob. The output is still the same. I dont know where am wrong? The folders are being created, files are being created, the complete file name is not being listed only the extension is printed which is ".fasta".

      #!/usr/bin/perl use Cwd; use strict; use warnings; my $path_to_fastaSeqs = $ARGV[0]; my $path_to_fragmentScript = $ARGV[1]; my %seqInfo = (); &storeInHash; &createFolderAndFiles; &callFragmentScript; sub storeInHash { open(FILE, "$path_to_fastaSeqs") or die("cannot open file"); { while(<FILE>) { my $line = $_; if ($line =~ />.*/) { #print "$&\n"; } else { #print "$line\n"; } $seqInfo{$&} = $line; } close(FILE); } } sub createFolderAndFiles { my $dir = getcwd; while(my ($key, $value) = each(%seqInfo)) { my $nameWithoutFastaSign = $key; $nameWithoutFastaSign =~ tr/>//d; mkdir $nameWithoutFastaSign, 0777; open(FILEOUT, ">$dir/$nameWithoutFastaSign/$nameWithoutFastaSign.fasta +"); { print FILEOUT "$key\n$value\n"; } } } sub callFragmentScript { while(my $key = each %seqInfo) { my $nameWithoutFastaSign = $key; $nameWithoutFastaSign =~ tr/>//d; my $currentDir = getcwd; chdir "$currentDir/$nameWithoutFastaSign"; my $changedDir = getcwd; print "changed directory is: $changedDir\n"; while(my $fastaFile = glob("*.fasta")) { print "$fastaFile\n"; } chdir "$currentDir"; } }

      output is

      changed directory is: /home/ammarah/Documents/code/1elwA .fasta changed directory is: /home/ammarah/Documents/code/1ghwA .fasta changed directory is: /home/ammarah/Documents/code/1flwA .fasta

        storeInHash seems problematic. I have seen a .fasta file, and it looks like  >gi|21040368|ref| ... , and on my platform there is no way to create a file/directory with a | character in the name, so what Platform/Filesystem are you on?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://946333]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (10)
As of 2014-07-23 11:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (140 votes), past polls