Re: error opening file with open function
Instead of taking the file as an input parameter, try to parse the file by hard coding its name in the script and let us know the outcome. Cheers, Parag On Sun, Nov 7, 2010 at 6:00 PM, J M jmd...@gmail.com wrote: Hi all, I'm having great issues with my program at the moment... it is supposed to take a csv file and parse it, with the output being stored in a MySQL database. The problem I'm having is that the file I need to parse ('ish_history.csv') will not open in the program for some reason. I know the file exists, I'm on a linux system and the file is world-readable, and I've tried placing the file in the directory the script is run from AS WELL as using full path names to said file... all has resulted in no luck, and I'm getting the No such file or directory error. Here is my code so far: [code] #! /usr/bin/perl # use DBI; use DBD::mysql; use Text::CSV; #configure variables my $host = localhost; my $db = somefakedatabase; my $table = somefaketable; my $user = somefakeuser; my $password = yeahrightlikethisismyrealpasswordLOL; #connect to the database my $dbc = dbi:mysql:$db:$host; if(my $dbh = DBI-connect($dbc, $user, $password) or die Can't connect to the database: $DBI::errstr\n) { print Connected to the database!; } #setup parser my $csv = Text::CSV-new(); print What is the name of the file? : ; chomp(my $file = STDIN); open CSV, , $file or die Can't open the file \$file\: $!; while(CSV) { next if ($. == 1); if ($csv-parse($_)) { my @columns = $csv-fields(); my $myquery = $dbh-prepare('insert into $table (station_number,WBAN_number,station_name,country,fips,state,ICAO_callsign,lattitude,longitude,elevation) values ($columns[0], $columns[1], **truncated** $myquery-execute(); $dbh-disconnect(); } else { my $err = $csv-error_input; print Failed to parse line: $err; } } close CSV; closedir TMPDIR; [\code] Any ideas as to why this isn't working?
Re: error opening file with open function
Use strict and warnings. Probably should also use parentheses: open(CSV, $file) On Nov 7, 2010 9:01 PM, J M jmd...@gmail.com wrote: Hi all, I'm having great issues with my program at the moment... it is supposed to take a csv file and parse it, with the output being stored in a MySQL database. The problem I'm having is that the file I need to parse ('ish_history.csv') will not open in the program for some reason. I know the file exists, I'm on a linux system and the file is world-readable, and I've tried placing the file in the directory the script is run from AS WELL as using full path names to said file... all has resulted in no luck, and I'm getting the No such file or directory error. Here is my code so far: [code] #! /usr/bin/perl # use DBI; use DBD::mysql; use Text::CSV; #configure variables my $host = localhost; my $db = somefakedatabase; my $table = somefaketable; my $user = somefakeuser; my $password = yeahrightlikethisismyrealpasswordLOL; #connect to the database my $dbc = dbi:mysql:$db:$host; if(my $dbh = DBI-connect($dbc, $user, $password) or die Can't connect to the database: $DBI::errstr\n) { print Connected to the database!; } #setup parser my $csv = Text::CSV-new(); print What is the name of the file? : ; chomp(my $file = STDIN); open CSV, , $file or die Can't open the file \$file\: $!; while(CSV) { next if ($. == 1); if ($csv-parse($_)) { my @columns = $csv-fields(); my $myquery = $dbh-prepare('insert into $table (station_number,WBAN_number,station_name,country,fips,state,ICAO_callsign,lattitude,longitude,elevation) values ($columns[0], $columns[1], **truncated** $myquery-execute(); $dbh-disconnect(); } else { my $err = $csv-error_input; print Failed to parse line: $err; } } close CSV; closedir TMPDIR; [\code] Any ideas as to why this isn't working?
Re: error opening file with open function
On Sun, Nov 7, 2010 at 8:13 PM, shawn wilson ag4ve...@gmail.com wrote: Use strict and warnings. Probably should also use parentheses: open(CSV, $file) On Nov 7, 2010 9:01 PM, J M jmd...@gmail.com wrote: Hi all, I'm having great issues with my program at the moment... it is supposed to take a csv file and parse it, with the output being stored in a MySQL database. The problem I'm having is that the file I need to parse ('ish_history.csv') will not open in the program for some reason. I know the file exists, I'm on a linux system and the file is world-readable, and I've tried placing the file in the directory the script is run from AS WELL as using full path names to said file... all has resulted in no luck, and I'm getting the No such file or directory error. Here is my code so far: [code] #! /usr/bin/perl # use DBI; use DBD::mysql; use Text::CSV; #configure variables my $host = localhost; my $db = somefakedatabase; my $table = somefaketable; my $user = somefakeuser; my $password = yeahrightlikethisismyrealpasswordLOL; #connect to the database my $dbc = dbi:mysql:$db:$host; if(my $dbh = DBI-connect($dbc, $user, $password) or die Can't connect to the database: $DBI::errstr\n) { print Connected to the database!; } #setup parser my $csv = Text::CSV-new(); print What is the name of the file? : ; chomp(my $file = STDIN); open CSV, , $file or die Can't open the file \$file\: $!; while(CSV) { next if ($. == 1); if ($csv-parse($_)) { my @columns = $csv-fields(); my $myquery = $dbh-prepare('insert into $table (station_number,WBAN_number,station_name,country,fips,state,ICAO_callsign,lattitude,longitude,elevation) values ($columns[0], $columns[1], **truncated** $myquery-execute(); $dbh-disconnect(); } else { my $err = $csv-error_input; print Failed to parse line: $err; } } close CSV; closedir TMPDIR; [\code] Any ideas as to why this isn't working? I should've mentioned it, but I did both of those things as well in fact, those were in the first version of the program and I thought maybe taking THEM out would help lol... thank you though!
Re: error opening file with open function
I did hardcode the name in the first version of the script... in two forms: 'file.txt' and '/home/path/to/myfile/file.txt'. Neither made any difference. Thank you for assisting! On Sun, Nov 7, 2010 at 8:12 PM, Parag Kalra paragka...@gmail.com wrote: Instead of taking the file as an input parameter, try to parse the file by hard coding its name in the script and let us know the outcome. Cheers, Parag On Sun, Nov 7, 2010 at 6:00 PM, J M jmd...@gmail.com wrote: Hi all, I'm having great issues with my program at the moment... it is supposed to take a csv file and parse it, with the output being stored in a MySQL database. The problem I'm having is that the file I need to parse ('ish_history.csv') will not open in the program for some reason. I know the file exists, I'm on a linux system and the file is world-readable, and I've tried placing the file in the directory the script is run from AS WELL as using full path names to said file... all has resulted in no luck, and I'm getting the No such file or directory error. Here is my code so far: [code] #! /usr/bin/perl # use DBI; use DBD::mysql; use Text::CSV; #configure variables my $host = localhost; my $db = somefakedatabase; my $table = somefaketable; my $user = somefakeuser; my $password = yeahrightlikethisismyrealpasswordLOL; #connect to the database my $dbc = dbi:mysql:$db:$host; if(my $dbh = DBI-connect($dbc, $user, $password) or die Can't connect to the database: $DBI::errstr\n) { print Connected to the database!; } #setup parser my $csv = Text::CSV-new(); print What is the name of the file? : ; chomp(my $file = STDIN); open CSV, , $file or die Can't open the file \$file\: $!; while(CSV) { next if ($. == 1); if ($csv-parse($_)) { my @columns = $csv-fields(); my $myquery = $dbh-prepare('insert into $table (station_number,WBAN_number,station_name,country,fips,state,ICAO_callsign,lattitude,longitude,elevation) values ($columns[0], $columns[1], **truncated** $myquery-execute(); $dbh-disconnect(); } else { my $err = $csv-error_input; print Failed to parse line: $err; } } close CSV; closedir TMPDIR; [\code] Any ideas as to why this isn't working?
Re: error opening file with open function
Do an 'ls -l' and a 'whoami' and post the result. Also post the error your program gives. On Nov 7, 2010 9:19 PM, J M jmd...@gmail.com wrote: I did hardcode the name in the first version of the script... in two forms: 'file.txt' and '/home/path/to/myfile/file.txt'. Neither made any difference. Thank you for assisting! On Sun, Nov 7, 2010 at 8:12 PM, Parag Kalra paragka...@gmail.com wrote: Instead of taking the file as an input parameter, try to parse the file by hard coding its name in the script and let us know the outcome. Cheers, Parag On Sun, Nov 7, 2010 at 6:00 PM, J M jmd...@gmail.com wrote: Hi all, I'm having great issues with my program at the moment... it is supposed to take a csv file and parse it, with the output being stored in a MySQL database. The problem I'm having is that the file I need to parse ('ish_history.csv') will not open in the program for some reason. I know the file exists, I'm on a linux system and the file is world-readable, and I've tried placing the file in the directory the script is run from AS WELL as using full path names to said file... all has resulted in no luck, and I'm getting the No such file or directory error. Here is my code so far: [code] #! /usr/bin/perl # use DBI; use DBD::mysql; use Text::CSV; #configure variables my $host = localhost; my $db = somefakedatabase; my $table = somefaketable; my $user = somefakeuser; my $password = yeahrightlikethisismyrealpasswordLOL; #connect to the database my $dbc = dbi:mysql:$db:$host; if(my $dbh = DBI-connect($dbc, $user, $password) or die Can't connect to the database: $DBI::errstr\n) { print Connected to the database!; } #setup parser my $csv = Text::CSV-new(); print What is the name of the file? : ; chomp(my $file = STDIN); open CSV, , $file or die Can't open the file \$file\: $!; while(CSV) { next if ($. == 1); if ($csv-parse($_)) { my @columns = $csv-fields(); my $myquery = $dbh-prepare('insert into $table (station_number,WBAN_number,station_name,country,fips,state,ICAO_callsign,lattitude,longitude,elevation) values ($columns[0], $columns[1], **truncated** $myquery-execute(); $dbh-disconnect(); } else { my $err = $csv-error_input; print Failed to parse line: $err; } } close CSV; closedir TMPDIR; [\code] Any ideas as to why this isn't working?
Re: error opening file with open function
J M wrote: -- Forwarded message -- From: J Mjmd...@gmail.com Date: Sun, Nov 7, 2010 at 8:27 PM Subject: Re: error opening file with open function To: shawn wilsonag4ve...@gmail.com shawn, I pretty sure it's not a permissions thing but here you go: [code] (jmd...@darkstar)-(0)-(07:23 PM Sun Nov 07)- (~/global_stats)-(5 files, 2.4Mb)-- ls -lh total 2.3M -rw-r--r-- 1 jmd9qs sys 203 2010-11-07 12:10 globals.php -rwxr-xr-x 1 jmd9qs sys 1.6K 2010-11-07 18:50 globals.pl* -rwxr-xr-x 1 jmd9qs sys 1.6K 2010-11-07 18:48 globals.pl~* -rw-r--r-- 1 jmd9qs sys 864 2010-11-07 12:39 import.php -rwxrwxrwx 1 jmd9qs sys 2.3M 2010-11-07 14:15 ish-history.csv* (jmd...@darkstar)-(0)-(07:23 PM Sun Nov 07)- (~/global_stats)-(5 files, 2.4Mb)-- whoami jmd9qs [\code] And here's the error I get [code] (jmd...@darkstar)-(0)-(07:23 PM Sun Nov 07)- (~/global_stats)-(5 files, 2.4Mb)-- ./globals.pl Connected to the database!What is the name of the file (probably /home/jmd9qs/global_stats/ish_history.csv)? : /home/jmd9qs/global_stats/ish_history.csv Can't open the file /home/jmd9qs/global_stats/ish_history.csv: No such file or directory at ./globals.pl line 28,STDIN line 1. (jmd...@darkstar)-(0)-(07:25 PM Sun Nov 07)- (~/global_stats)-(5 files, 2.4Mb)-- [\code] Your file is named 'ish-history.csv' (with a hythen between 'ish' and 'history') but you are trying to open 'ish_history.csv' (with an underscore between 'ish' and 'history'). John -- Any intelligent fool can make things bigger and more complex... It takes a touch of genius - and a lot of courage to move in the opposite direction. -- Albert Einstein -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: error opening file with open function
Ok now that John has helped me realize that I've been acting like a fool for the past 3 hours, I'm getting this error: DBD::mysql::st execute failed: MySQL server has gone away at [whatever line number] Thanks for your help, guys... currently I'm changing the script to use placeholders and $dbh-do() instead of $dbh-prepare() and $dbh-excecute(). I'll post updated code when that is done. On Sun, Nov 7, 2010 at 8:58 PM, John W. Krahn jwkr...@shaw.ca wrote: J M wrote: -- Forwarded message -- From: J Mjmd...@gmail.com Date: Sun, Nov 7, 2010 at 8:27 PM Subject: Re: error opening file with open function To: shawn wilsonag4ve...@gmail.com shawn, I pretty sure it's not a permissions thing but here you go: [code] (jmd...@darkstar)-(0)-(07:23 PM Sun Nov 07)- (~/global_stats)-(5 files, 2.4Mb)-- ls -lh total 2.3M -rw-r--r-- 1 jmd9qs sys 203 2010-11-07 12:10 globals.php -rwxr-xr-x 1 jmd9qs sys 1.6K 2010-11-07 18:50 globals.pl* -rwxr-xr-x 1 jmd9qs sys 1.6K 2010-11-07 18:48 globals.pl~* -rw-r--r-- 1 jmd9qs sys 864 2010-11-07 12:39 import.php -rwxrwxrwx 1 jmd9qs sys 2.3M 2010-11-07 14:15 ish-history.csv* (jmd...@darkstar)-(0)-(07:23 PM Sun Nov 07)- (~/global_stats)-(5 files, 2.4Mb)-- whoami jmd9qs [\code] And here's the error I get [code] (jmd...@darkstar)-(0)-(07:23 PM Sun Nov 07)- (~/global_stats)-(5 files, 2.4Mb)-- ./globals.pl Connected to the database!What is the name of the file (probably /home/jmd9qs/global_stats/ish_history.csv)? : /home/jmd9qs/global_stats/ish_history.csv Can't open the file /home/jmd9qs/global_stats/ish_history.csv: No such file or directory at ./globals.pl line 28,STDIN line 1. (jmd...@darkstar)-(0)-(07:25 PM Sun Nov 07)- (~/global_stats)-(5 files, 2.4Mb)-- [\code] Your file is named 'ish-history.csv' (with a hythen between 'ish' and 'history') but you are trying to open 'ish_history.csv' (with an underscore between 'ish' and 'history'). John -- Any intelligent fool can make things bigger and more complex... It takes a touch of genius - and a lot of courage to move in the opposite direction. -- Albert Einstein -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: error opening file with open function
I was able to figure it out. Here is the update (probably not final) code for anyone interested: [code] #! /usr/bin/perl # use DBI; use DBD::mysql; use Text::CSV; use strict; #configure variables my $host = localhost; my $db = global_stats; my $table = station_information; my $user = globalstats; my $password = algoreliesLOL; #connect to the database my $dbc = dbi:mysql:$db:$host; my $dbh; if($dbh = DBI-connect($dbc, $user, $password) or die Can't connect to the database: $DBI::errstr\n) { print Connected to the database!; } #setup parser my $csv = Text::CSV-new(); print What is the name of the file? (ENTER for default) : ; chomp(my $file = STDIN); if($file -eq \n) { $file = '/home/jmd9qs/global_stats/ish-history.csv'; } open CSV, , $file or die Can't open the file \$file\: $!; while(CSV) { next if ($. == 1); if ($csv-parse($_)) { my @columns = $csv-fields(); my $myquery = $dbh-do(insert into station_information (station_number,WBAN_number,station_name,country,fips,state,ICAO_callsign,lattitude,longitude,elevation) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?), {}, $columns[0], $columns[1], $columns[2], $columns[3], $columns[4], $columns[5], $columns[6], $columns[7], $columns[8], $columns[9]); } else { my $err = $csv-error_input; print Failed to parse line: $err; } } close CSV; $dbh-disconnect(); [\code] Thanks again for the help. jmd9qs On Sun, Nov 7, 2010 at 9:25 PM, J M jmd...@gmail.com wrote: Ok now that John has helped me realize that I've been acting like a fool for the past 3 hours, I'm getting this error: DBD::mysql::st execute failed: MySQL server has gone away at [whatever line number] Thanks for your help, guys... currently I'm changing the script to use placeholders and $dbh-do() instead of $dbh-prepare() and $dbh-excecute(). I'll post updated code when that is done. On Sun, Nov 7, 2010 at 8:58 PM, John W. Krahn jwkr...@shaw.ca wrote: J M wrote: -- Forwarded message -- From: J Mjmd...@gmail.com Date: Sun, Nov 7, 2010 at 8:27 PM Subject: Re: error opening file with open function To: shawn wilsonag4ve...@gmail.com shawn, I pretty sure it's not a permissions thing but here you go: [code] (jmd...@darkstar)-(0)-(07:23 PM Sun Nov 07)- (~/global_stats)-(5 files, 2.4Mb)-- ls -lh total 2.3M -rw-r--r-- 1 jmd9qs sys 203 2010-11-07 12:10 globals.php -rwxr-xr-x 1 jmd9qs sys 1.6K 2010-11-07 18:50 globals.pl* -rwxr-xr-x 1 jmd9qs sys 1.6K 2010-11-07 18:48 globals.pl~* -rw-r--r-- 1 jmd9qs sys 864 2010-11-07 12:39 import.php -rwxrwxrwx 1 jmd9qs sys 2.3M 2010-11-07 14:15 ish-history.csv* (jmd...@darkstar)-(0)-(07:23 PM Sun Nov 07)- (~/global_stats)-(5 files, 2.4Mb)-- whoami jmd9qs [\code] And here's the error I get [code] (jmd...@darkstar)-(0)-(07:23 PM Sun Nov 07)- (~/global_stats)-(5 files, 2.4Mb)-- ./globals.pl Connected to the database!What is the name of the file (probably /home/jmd9qs/global_stats/ish_history.csv)? : /home/jmd9qs/global_stats/ish_history.csv Can't open the file /home/jmd9qs/global_stats/ish_history.csv: No such file or directory at ./globals.pl line 28,STDIN line 1. (jmd...@darkstar)-(0)-(07:25 PM Sun Nov 07)- (~/global_stats)-(5 files, 2.4Mb)-- [\code] Your file is named 'ish-history.csv' (with a hythen between 'ish' and 'history') but you are trying to open 'ish_history.csv' (with an underscore between 'ish' and 'history'). John -- Any intelligent fool can make things bigger and more complex... It takes a touch of genius - and a lot of courage to move in the opposite direction. -- Albert Einstein -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: error opening file with open function
Cross thread posted at http://forums.devshed.com/perl-programming-6/error-opening-csv-file-with-open-function-761095.html J M wrote: I was able to figure it out. Here is the update (probably not final) code for anyone interested: [code] #! /usr/bin/perl # use DBI; use DBD::mysql; use Text::CSV; use strict; #configure variables my $host = localhost; my $db = global_stats; my $table = station_information; my $user = globalstats; my $password = algoreliesLOL; #connect to the database my $dbc = dbi:mysql:$db:$host; my $dbh; if($dbh = DBI-connect($dbc, $user, $password) or die Can't connect to the database: $DBI::errstr\n) { print Connected to the database!; } #setup parser my $csv = Text::CSV-new(); print What is the name of the file? (ENTER for default) : ; chomp(my $file = STDIN); if($file -eq \n) { $file = '/home/jmd9qs/global_stats/ish-history.csv'; } open CSV, , $file or die Can't open the file \$file\: $!; while(CSV) { next if ($. == 1); if ($csv-parse($_)) { my @columns = $csv-fields(); my $myquery = $dbh-do(insert into station_information (station_number,WBAN_number,station_name,country,fips,state,ICAO_callsign,lattitude,longitude,elevation) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?), {}, $columns[0], $columns[1], $columns[2], $columns[3], $columns[4], $columns[5], $columns[6], $columns[7], $columns[8], $columns[9]); } else { my $err = $csv-error_input; print Failed to parse line: $err; } } close CSV; $dbh-disconnect(); [\code] Thanks again for the help. jmd9qs D:\perlperl -c jmd9qs.pl syntax error at jmd9qs.pl line 24, near -eq jmd9qs.pl had compilation errors. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Error opening file?
Hi Billy, can't see anything wrong with the code, except that it assumes that the file is in the current directory. It's not something as daft as you 'cd'ing to that directory before you ran in debug mode is it? Gary On Friday 27 April 2001 3:44 am, Billy Joedono wrote: Hi all, Below is a piece of code central to my problem. I've used this a lot of time without problem, but on this occasion, it fails me. Whenever I run the script, it ends with the error bash: ./test.pl: No such file or directory. The funny thing is, if I activate the debug option (use #!/usr/bin/perl -d) and step through the code, it does what I want, no errors given there. Can somebody points me to the cause of this? Seeing that it works when debugged suggests that it is not permission problem, or is it? I know it's going to be something simple that I miss. Anyway, I am using Red Hat 6.2 and perl 5.005_03. #!/usr/bin/perl open (FLT, once_filter.txt) || warn can not open filter file: $!; while (FLT) { # chomp; $Filter= $_; printf Search filter is $Filter\n; snip ... } close (FLT) || warn can not close filter file: $!; * ** Thanks and regards, Billy Joedono Courtesy comes by default, but respect I gotta earn * ** -- Gary Stainburn This email does not contain private or confidential material as it may be snooped on by interested government parties for unknown and undisclosed purposes - Regulation of Investigatory Powers Act, 2000
Re: Error opening file? - Solved!
Hi Billy, unix2dos #!/usr/local/bin/perl -w while (STDIN) { chomp; if ( $0=~m/dos2unix$/ ) { print $_\n; } else { print $_\r\n; } } dos2unix #!/usr/local/bin/perl -w while (STDIN) { chomp; if ( $0=~m/dos2unix$/ ) { print $_\n; } else { print $_\r\n; } } On Friday 27 April 2001 7:43 am, Billy Joedono wrote: Hi, Thanks to all who commented. They are all correct. John Joseph's suggestion to use #!/usr/bin/per; -w would work too. But Jason's suggestion below pointed me to the solution. When the command perl test.pl work, right there I realize file format might be suspect. I pulled out my HEX editor and there they were, the CRLF pairs. I didn't show in Linux vi the way it would in Solaris vi, neither would it in the GNU editor (which I use). Convert to regular Unix format and problem solved! More question: how can I force vi to show these, or better yet, how can I get dos2unix or unix2dos in Linux? The down-side of Linux understanding much of Windowz is that you never get subjected to these little finicky differences until you're struck by them. At 01:24 PM 4/27/2001 +1000, King, Jason wrote: can you run it manually by passing it to the interpreter perl test.pl ? -- jason king A Canadian law states that citizens may not publicly remove bandages. - http://dumblaws.com/ * ** Thanks and regards, Billy Joedono Courtesy comes by default, but respect I gotta earn * ** -- Gary Stainburn This email does not contain private or confidential material as it may be snooped on by interested government parties for unknown and undisclosed purposes - Regulation of Investigatory Powers Act, 2000
RE: Error opening file?
Billy Joedono writes .. Below is a piece of code central to my problem. I've used this a lot of time without problem, but on this occasion, it fails me. Whenever I run the script, it ends with the error bash: ./test.pl: No such file or directory. The funny thing is, if I activate the debug option (use #!/usr/bin/perl -d) and step through the code, it does what I want, no errors given there. can you run it manually by passing it to the interpreter perl test.pl ? the permissions should be (at least) 755 .. ie. chmod 755 test.pl but you're right .. the fact that it works in the debugger should mean that's not the problem I assume that you aren't doing anything crazy like having a blank line before the shebang line .. because obviously that'll also create problems (but you'd have to be really crazy to delete the blank line when adding the '-d' option) other than ensuring that the shebang line is correct (again - you'd have to be crazy to be changing it without realising when you add the '-d' option) there doesn't seem to be anything obvious -- jason king A Canadian law states that citizens may not publicly remove bandages. - http://dumblaws.com/