[英]Perl LWP::Simple::getstore How to check if the file exists in destination directory
[英]Perl & LWP: getstore() will only save a file if I don't specify a directory
我可能缺少一些簡單的東西,但我的getstore()運行得很好,可以將“ .png”文件保存到Windows PC中,如下所示:
getstore("$url","$filename");
這樣可以有效地將文件完美下載到包含Perl腳本的目錄中。 但是,如果我嘗試將其保存到“ newdir”的子文件夾中,則不會下載任何內容:
getstore("$url","newdir\\$filename");
知道我在哪里出錯或如何調試嗎? 我嘗試打印目錄,看起來不錯:
print "newdir\\$filename";
但是,一旦腳本運行,“ newdir”中就沒有任何內容。 提前致謝。
使用open(my $fh, ">", $arg)
文件,其中$arg
包含您傳遞的值。 因此,它絕對會接受相對路徑。
>dir /b newdir
>perl -MLWP::Simple=getstore -e"getstore('http://stackoverflow.com/', 'newdir\\so.html')"
>dir /b newdir
so.html
也許$filename
無效(例如,以換行符結尾)。 也許當前目錄不是您想的那樣(它可以是包含腳本的目錄以外的目錄)。 也許執行腳本的用戶無權訪問該目錄。 您應該能夠使用以下方法獲得更多信息:
use LWP::UserAgent qw( );
my $ua = LWP::UserAgent->new();
my $request = HTTP::Request->new(GET => $url);
my $response = $ua->request($request, "newdir\\$filename");
die($response->status_line)
if !$response->is_success;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.