Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

No such file or directory error

by Anonymous Monk
on Aug 22, 2012 at 04:40 UTC ( #988946=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I try to download filings from SEC website using the code provided on the website: http://www.wrds.us/index.php/tutorial/view/26 I received an error "No such file or directory at FILE.pl line 45, <dlthis> line 6868." I have a text file containing url links to the forms that I require. There are 6867 url and one header. So in total there are 6868 lines in the txt file. I am new to Perl. Any help is much appreciated.

#!/usr/bin/perl use LWP; use HTTP::Request; sub get_http { my $url = shift; my $request = HTTP::Request->new(GET => $url); my $response = $ua->request($request); if (!$response->is_success) { print STDERR "GET '%s' failed: %s\n", $url, $response->status_line; return undef; } return $response->content; } # user agent object for handling HTTP requests my $ua = LWP::UserAgent->new; # if you only want a portion of the filing, un-comment the next line #$ua->max_size(50000); # 50k byte limit ######################### write dir , use "\\" and not "\", for exampl +e: "C:\\temp" $write_dir = "C:\\temp"; ######################### write dir ######################### filename with urls (put in same directory as + script) open dlthis, "c_10K_list.txt" or die $!; ######################### filename with urls (put in same directory as + script) ######################### log open LOG , ">download_log.txt" or die $!; ######################### log my @file = <dlthis>; foreach $line (@file) { #CIK, filename, blank is not used (included because it will capture t +he newline) ($CIK, $get_file, $blank) = split (",", $line); $get_file = "http://www.sec.gov/Archives/" . $get_file; $_ = $get_file; if ( /([0-9|-]+).txt/ ) { $filename = $write_dir . "/" . $CIK . ".txt"; open OUT, ">$filename" or die $!; print "file $CIK \n"; my $request = HTTP::Request->new(GET => $get_file); my $response =$ua->get($get_file ); $p = $response->content; if ($p) { print OUT $p; close OUT; } else { #error logging print LOG "error in $filename - $CIK \n" ; } } } close LOG;

Replies are listed 'Best First'.
Re: No such file or directory error
by CountZero (Bishop) on Aug 22, 2012 at 06:12 UTC
    Change line 45 to
    open OUT, '>', $filename or die ">$filename< error - $!";
    Now you can see what filename gives you problems. More likely than not you will see it is an invalid filename (such as containing a linefeed or other illegal character).

    PS1: I also changed your open to the three arguments variant. It would even be better if you use lexical variables as filehandles.

    PS2: This script is not really "State of the Art", I guess it must be rather old already or based upon an old script, so try not to pick up too many bad habits by studying it.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics

      Thanks

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2020-04-03 04:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The most amusing oxymoron is:
















    Results (26 votes). Check out past polls.

    Notices?