[英]Why I can not open files to read or write in perl?
I am learning Perl by using vs code.我正在使用 vs 代码学习 Perl。 I am trying to open file.pep and read from it but every time I get that the path is not found.
我正在尝试打开 file.pep 并从中读取,但每次我发现找不到路径。 I have put the protein.pep and code.pl in the same folder.
我已将 protein.pep 和 code.pl 放在同一个文件夹中。
here is the protein.pep file这是 protein.pep 文件
MNIDDKLEGLFLKCGGIDEMQSSRTMVVMGGVSGQSTVSGELQD
SVLQDRSMPHQEILAADEVLQESEMRQQDMISHDELMVHEETVKNDEEQMETHERLPQ
GLQYALNVPISVKQEITFTDVSEQLMRDKKQIR
with path D:\bioinformatics\protein.pep使用路径 D:\bioinformatics\protein.pep
here is my code.pl file这是我的 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;
and its path is D:\bioinformatics\code.pl它的路径是 D:\bioinformatics\code.pl
I am getting this output "The system cannot find the path specified."我得到这个 output “系统找不到指定的路径。”
You have in your code:您的代码中有:
$proteinfilename = 'protein.pep';
open(PROTEINFILE, $proteinfilename)or die "Can't open '$seq': $!";
First, change the error message to tell you which file the open
wants:首先,更改错误消息以告诉您
open
需要哪个文件:
open(PROTEINFILE, $proteinfilename) or die "Can't open '$proteinfilename': $!";
You only give the open
the relative path name.你只给
open
相对路径名。 I bet it works with the full path name:我敢打赌它适用于完整的路径名:
$proteinfilename = 'D:\\bioinformatics\\protein.pep';
open(PROTEINFILE, $proteinfilename) or die "Can't open '$proteinfilename': $!";
If you still have problems with that, post the actual output of the program.如果您仍然有问题,请发布程序的实际 output。
I'm guessing that the problem is with your IDE and the current working directory.我猜问题出在您的 IDE 和当前工作目录上。 A program does not automatically work in the directory in which you store it.
程序不会自动在您存储它的目录中运行。 You can use the
Cwd
module that comes with Perl to see where your IDE starts you:您可以使用 Perl 随附的
Cwd
模块来查看您的 IDE 从哪里开始:
use Cwd qw(getcwd);
print "I'm in " . getcwd() . "\n";
If you want your program to be in a particular directory as it does its work,如果您希望程序在工作时位于特定目录中,
chdir $some_directory or die "Could not change to '$some_directory': $!";
You might choose that directory to be in the same as that of the program.您可以选择该目录与程序的目录相同。 The
FindBin
module that comes with Perl is handy for telling you where that is: Perl 附带的
FindBin
模块可以方便地告诉您它在哪里:
use FindBin;
chdir $FindBin::Bin;
Perl has various better ways of doing things even though it supports almost everything you could do 30 years ago. Perl 有各种更好的做事方式,尽管它几乎支持你 30 年前可以做的所有事情。 The three argument
open
that denotes the file mode is a bit safer.表示文件模式的三个参数
open
更安全一些。 And, Perl v5.36 is about the disable user-defined bareword filehandles when you specify use v5.36
, so get into the habit of using a lexical filehandle.而且, Perl v5.36 是关于在您指定
use v5.36
时禁用用户定义的裸字文件句柄,因此请养成使用词法文件句柄的习惯。
open my $protein_fh, '<', $proteinfilename or die "Can't open '$proteinfilename': $!";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.