简体   繁体   中英

Why I can not open files to read or write in perl?

I am learning Perl by using vs code. I am trying to open file.pep and read from it but every time I get that the path is not found. I have put the protein.pep and code.pl in the same folder.

here is the protein.pep file

MNIDDKLEGLFLKCGGIDEMQSSRTMVVMGGVSGQSTVSGELQD
SVLQDRSMPHQEILAADEVLQESEMRQQDMISHDELMVHEETVKNDEEQMETHERLPQ
GLQYALNVPISVKQEITFTDVSEQLMRDKKQIR

with path D:\bioinformatics\protein.pep

here is my code.pl file

#!/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

I am getting this output "The system cannot find the path specified."

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(PROTEINFILE, $proteinfilename) or die "Can't open '$proteinfilename': $!";

You only give the open the relative path name. 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.

You aren't where you think you are

I'm guessing that the problem is with your IDE and the current working directory. 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:

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:

use FindBin;
chdir $FindBin::Bin;

A bit more modern

Perl has various better ways of doing things even though it supports almost everything you could do 30 years ago. The three argument open that denotes the file mode is a bit safer. 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.

open my $protein_fh, '<', $proteinfilename or die "Can't open '$proteinfilename': $!";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM