我正在使用vs代码学习Perl。我试图打开文件.pep并从中读取,但是每次我得到该路径时都找不到。我将protein.pep和code.pl放在同一个文件夹中。
这是protein.pep文件
MNIDDKLEGLFLKCGGIDEMQSSRTMVVMGGVSGQSTVSGELQD
SVLQDRSMPHQEILAADEVLQESEMRQQDMISHDELMVHEETVKNDEEQMETHERLPQ
GLQYALNVPISVKQEITFTDVSEQLMRDKKQIR带路径D:\bioinformatics\protein.pep
这是我的code.pl文件
#!/usr/bin/perl -w
$proteinfilename = 'protein.pep';
open(PROTEINFILE, $proteinfilename)or die "Can't open '$seq': $!";
# First line
$protein = <PROTEINFILE>;
# Print the protein onto the screen
print "\nHere is the first line of the protein file:\n\n";
print $protein;
# Second line
$protein = <PROTEINFILE>;
# Print the protein onto the screen
print "\nHere is the second line of the protein file:\n\n";
print $protein;
# Third line
$protein = <PROTEINFILE>;
# Print the protein onto the screen
print "\nHere is the third line of the protein file:\n\n";
print $protein;它的路径是D:\bioinformatics\code.pl
我得到这个输出“系统找不到指定的路径”。
发布于 2021-12-21 12:15:27
在你的代码中:
$proteinfilename = 'protein.pep';
open(PROTEINFILE, $proteinfilename)or die "Can't open '$seq': $!";首先,更改错误消息,告诉您open需要哪个文件:
open(PROTEINFILE, $proteinfilename) or die "Can't open '$proteinfilename': $!";您只为open提供相对路径名。我打赌它适用于完整的路径名:
$proteinfilename = 'D:\\bioinformatics\\protein.pep';
open(PROTEINFILE, $proteinfilename) or die "Can't open '$proteinfilename': $!";如果你仍然有这个问题,发布程序的实际输出。
你不在你认为你在的地方
我猜问题在于IDE和当前的工作目录。程序不会自动在存储它的目录中工作。您可以使用Perl附带的Cwd模块来查看您的IDE从何处开始:
use Cwd qw(getcwd);
print "I'm in " . getcwd() . "\n";如果你希望你的程序在一个特定的目录中工作,
chdir $some_directory or die "Could not change to '$some_directory': $!";您可以选择该目录与程序的目录相同。Perl附带的FindBin模块可以方便地告诉您它在哪里:
use FindBin;
chdir $FindBin::Bin;更现代一点
Perl有各种更好的做事方法,尽管它支持30年前您可以做的几乎所有事情。表示文件模式的三个参数open比较安全。而且,Perl v5.36是关于在指定use v5.36时禁用用户定义的裸字文件句柄的,因此养成了使用词法文件句柄的习惯。
open my $protein_fh, '<', $proteinfilename or die "Can't open '$proteinfilename': $!";https://stackoverflow.com/questions/70434682
复制相似问题