Python: will not read a certain file in a for loop - for-loop

I have a directory containing files and they are all processed except one, file2.txt with my the_script.py script.
Independanty i ran a simple for line in file2.txt: print line and it worked just fine. The lines were printed. So the file is not the problem, it is formatted just as the other ones (automatically, output of another script).
Here is the_script.py :
#!/usr/bin/python
import os
import glob
#[...]rest of the code not dealing with the files in questions
for filename in glob.glob("outdir/*_mapp"): #i need to get all the files in outdir/ directory with the *_mapp extension
infilemapp=open(filename)
print "start"
print infilemapp #test, priting all filenames
organism=(filename.split("/", 1)[1])[:-5] # outdir/acorus.txt_mapp --> acorus.txt IRRELEVANT PARSING LINE
infilelpwe=organism+"_lpwe" #acorus.txt --> acorus.txt_lpwe IRRELEVANT PARSING LINE
for line in infilemapp:
print line
print "end"
What i expected is to get, for ALL files, "start, filename, filecontent, end". I get in console:
bash-4.3$ ./the_script.py
start
<open file 'outdir/file1.txt_mapp', mode 'r' at 0x7fb5795ec930>
['3R', '2F', '0R', '3F', '1R', '4F', '1F']
end
start
<open file 'outdir/file3.txt_mapp', mode 'r' at 0x7fb5795eca50>
['0R', '5R', '7R', '4R', '1F', '6R', '2R', '6F', '1R', '4F', '7F', '5F', '0F', '3R']
end
start
<open file 'outdir/file2.txt_mapp', mode 'r' at 0x7fb5795ec930>
end
As you can see, nothing is printed for file2.txt_mapp.
bash-4.3$ cat outdir/file2.txt_mapp
['5F', '0F', '2F', '6F', '3R', '5R', '6R', '4F', '1R', '4R', '6F']
The file is alphabetically in the middle of all files. Why does my script not work for this specific one? Please if you have any suggestions...

Related

shell script is not reading $HOME value properly

I have tried to fetch the value from another file with below code snippet
file=`echo "$(read_properties 'abc.config')"` // reading this input from other file
abc.config =$HOME/one/two
$HOME = /home/cbc
but when i use line number 1, Actual = $HOME/one/two
Expected : /home/cbc/one/two
where if i only print $HOME it gives me output as /home/cbc
but why it is not printing properly when i use in file variable

Windows batch loop saving results into a file line by line

I run a command on each file within a folder and I would like to write the result line by line in a text file
for %r in (*) do (magick identify -format "%f, %w, %h" %r >> out.txt)
(it returns the image name and its size)
Which gives:
1048.tif, 3175, 2802,1049.tif, 3175, 2802...
I would like something like
> 1048.tif, 3175, 2802
> 1049.tif, 3175, 2802...
I tried with echo before magick identify but it writes the command and not the result
With ImageMagick you can try putting a "\n" in your format string...
... -format "%f, %w, %h\n" ...
That will insert a line break after the height.

How to stop Ruby automatically adding carriage returns

I am having problems constructing text files from a Windows machine to be read on a Linux environment.
def test
my_file = Tempfile.new('filetemp.txt')
my_file.print "This is on the first line"
my_file.print "\x0A"
my_file.print "This is on the second line"
my_file.close
FileUtils.mv(my_file.path, "C:/Users/me/Desktop/Folder/test.usr")
end
0Ais the ASCII code for line feed, yet when I open the resulting file in Notepad++, I see it has appended CR and LF on the end of the line.
How do I add only a Line Feed as the new line character?
try setting the output separator $\ to \n.
def test
$\ = "\n"
my_file = Tempfile.new('filetemp.txt')
my_file.print "This is on the first line"
my_file.print "This is on the second line"
my_file.close
FileUtils.mv(my_file.path, "C:/Users/me/Desktop/Folder/test.usr")
end
or you should be able to use #write which will not add the output separator
def test
my_file = Tempfile.new('filetemp.txt')
my_file.write "This is on the first line"
my_file.write "\x0A"
my_file.write "This is on the second line"
my_file.close
FileUtils.mv(my_file.path, "C:/Users/me/Desktop/Folder/test.usr")
end
Opening the file in binary mode causes Ruby to 'suppress EOL <-> CRLF conversion on Windows' (see here).
The problem is that Tempfiles are automatically opened in w+ mode. I couldn't find a way to change this on creation of the Tempfile.
The answer is to change it after creation using binmode:
def test
my_file = Tempfile.new('filetemp.txt')
my_file.binmode
my_file.print "This is on the first line"
my_file.print "\x0A" # \n now also works as a newline character
my_file.print "This is on the second line"
my_file.close
FileUtils.mv(my_file.path, "C:/Users/me/Desktop/Folder/test.usr")
end

Figuring out pipes in python

i am currently writing a program in python and i am stuck. So my questtion is:
I have a program that reads a file and prints some lines to stdout like this:
#imports
import sys
#number of args
numArgs = len(sys.argv)
#ERROR if not enough args were committed
if numArgs <= 1:
sys.exit("Not enough arguments!")
#naming input file from args
Input = sys.argv[1]
#opening files
try:
fastQ = open(Input , 'r')
except IOError, e:
sys.exit(e)
#parsing through file
while 1:
#saving the lines
firstL = fastQ.readline()
secondL = fastQ.readline()
#you could maybe skip these lines to save ram
fastQ.readline()
fastQ.readline()
#make sure that there are no blank lines in the file
if firstL == "" or secondL == "":
break
#edit the Header to begin with '>'
firstL = '>' + firstL.replace('#' , '')
sys.stdout.write(firstL)
sys.stdout.write(secondL)
#close both files
fastQ.close()
Now i want to rewrite this program so that i can run a command line like : zcat "textfile" | python "myprogram" > "otherfile". So i looked around and found subprocess, but can't seem to figure out how to do it. thanks for your help
EDIT:
Now, if what you are doing is trying to write a Python script to orchestrate the execution of both zcat and myprogram, THEN you may need subprocess. – rchang
The intend is to have the "textfile" and the program on a cluster, so i dont need to copy any files from the cluster. i just want to login on the cluster and use the command:zcat "textfile" | python "myprogram" > "otherfile", so that the zcat and the program do their thing and i end up with "otherfile" on the cluster. hope you understand what i want to do.
Edit #2:
my solution
#imports
import sys
import fileinput
# Counter, maybe there is a better way
count = 0
# Iterieration over Input
for line in fileinput.input():
# Selection of Header
if count == 0 :
#Format the Header
newL = '>' + line.replace('#' , '')
# Print the Header without newline
sys.stdout.write(newL)
# Selection of Sequence
elif count == 1 :
# Print the Sequence
sys.stdout.write(line)
# Up's the Counter
count += 1
count = count % 4
THX
You could use fastQ = sys.stdin to read the input from stdin instead of the file or (more generally) fastQ = fileinput.input() to read from stdin and/or files specified on the command-line.
There is also fileinput.hook_compressed so that you don't need zcat and read the compressed file directly instead:
$ myprogram textfile >otherfile

How do you find the paths of linked images in Adobe Illustrator 9? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have an Illustrator file with linked images. I'd actually like to instead embed the images. I first have to know which files they are. How do I find out? I'm using Illustrator 9.
The first Illustrator version I ever used was 10, but, is there a links pallete in version 9? Try the window menu and look for "links". It has a few options there to search for the image you want, relink, open the original, etc.
Great to know the poster is set, but this doesn't answer the question. In CS3, if you double-click on the image in links palette, ti will bring up Link Info for the linked element, which shows the path to the file (provided it isn't longer than the window).
Maybe the older versions allow you to do this too.
Unfortunately, if you're dealing with a missing link element (which you ignored to fix upon opening the file), this field is blank. Illustrator sucks in comparison to InDesign for file packaging and linking. It would be nice if it could package files like InDesign, and store relative references to the external resources.
With newer versions of Illustrator you might be able to replace links to broken images using this script:
http://techblog.willshouse.com/2011/01/16/update-illustrator-linked-files-script/
I just had this problem in Illustrator CS4; a lot of my stuff was archived recently.
Click on the "missing" image in the artboard.
In the top left you will see the file name displayed.
Click "edit original" in the subsequent drop down menu. Illustrator will attempt to find the file, and flash a warning window "windows cannot find file" etc giving you the full file location.
This is useful as edit original is greyed out in the links window. And very useful for people like me who have a vast library of files.
I use the following perl script to keep track of linked images in Illustrator files. This is especially helpful for broken links, because it will still tell you the full path to the linked image by peeking inside the Illustrator file. It obviously does more than anyone here needs, but perhaps it will be useful. The help should explain how to use it. On my machine I have called it ailinkedfiles.pl and I have put it in ~/bin which is in my PATH.
#!/usr/bin/perl
# program to find the linked files inside an Adobe Illustrator file
require 5.004;
use File::Basename; # to extract a filename from a full path
use File::Find; # to build a list of files
use File::Spec; # Platform independent way to build paths
use vars qw/ %opt /; # for command line options - see init()
use strict;
init(); # process command line options
# Autoflush stdout
$|=1;
if ($opt{p}){
die "Did you really mean to call your script ".$opt{p}."!\n" if($opt{p} =~ /\.ai$/i);
print "Generating script file $opt{p}\n" if $opt{v};
open SCRIPT, "> $opt{p}";
}
die "No input specified; use ".basename($0)." -h for help\n" if(#ARGV==0);
my $arg; foreach $arg (#ARGV){
if(-d $arg){
# nb it is necesary to convert the directory specification
# to an absolute path to ensure that the open in &findLinkedFiles
# works properly during multi directory traversal
my $InDir=File::Spec->rel2abs($arg);
find(\&handleFind,$InDir);
} elsif (-f $arg) {
my $InDir=File::Spec->rel2abs(dirname($ARGV[0]));
&findLinkedFiles(File::Spec->rel2abs($ARGV[0]),$InDir) ;
# &findLinkedFiles(File::Spec->rel2abs($arg)) ;
}
}
sub init()
# copied from: http://www.cs.mcgill.ca/~abatko/computers/programming/perl/howto/getopts
{
use Getopt::Std; # to handle command line options
my $opt_string = 'hvlzdsftnp:ux:';
getopts( "$opt_string", \%opt ) or usage();
usage() if $opt{h};
}
# Print out usage information
sub usage()
{
print STDERR << "EOF";
Usage: $0 [OPTIONS] <AIFILE/DIR>
Parse an Adobe Illustrator file or (recursively) parse a directory of ai files
and print a list of the linked files to STDOUT. These could be piped to xargs eg:
$0 aifile.ai | xargs -I {} ln -vs
-h print this help
-v verbose ouput
-s print file names with short path
-d print current directory on each line
-n no-print (suppresses printing of linked file names)
-x <regex> exclude files whose full path matches regex
-l symlink in current directory if file linked from Illustrator file exists somewhere else
-f force symlink to overwrite existing target file
-t test run
-p <file> write commands to a script file
-u status of src and target
- doesn't exist
F plain file
L symbolic link
E exists (unknown file type)
Note that src is the link contained in the Illustrator file and
target is a file of the same name in the same directory as the Illustrator file
If the status is -- you will have problems in Illustrator
If the status is -F Illustrator will substitute the local file for the unavailable linked file
If the status is F- you can run this script with the -s option to make a symlink
If the status is FF then Illustrator will be happy
EOF
exit();
}
sub mysymlink{
my ($src,$targetdir)=#_;
my $target=File::Spec->catdir($targetdir,basename($src));
if(File::Spec->rel2abs($src) eq File::Spec->rel2abs($target)){
print "src and target identical for src=$src\n" if $opt{v};
return;
}
if(-e $src){
my $opts=$opt{f}?"-fsv":"-sv";
my $cmd="ln $opts \"$src\" \"$target\"";
myexec("$cmd");
} else {
print "No link made: $src doesn't exist\n" if $opt{v};
}
}
sub myexec {
my ($cmd) = #_;
if ($opt{t}){
print STDERR "test: $cmd\n";
} elsif ($opt{p}){
print SCRIPT $cmd,"\n";
} else {
# should get to see output with system
print STDERR "run: $cmd\n" if $opt{v};
return system $cmd;
}
}
sub mystatus{
my ($src,$targetdir)=#_;
my $target=File::Spec->catdir($targetdir,basename($src));
my ($ss,$ts)=("-","-");
$ss = "E" if(-e $src);
$ss = "F" if(-f $src);
$ss = "L" if(-l $src);
$ts = "E" if(-e $target);
$ts = "F" if(-f $target);
$ts = "L" if(-l $target);
return ($ss.$ts);
}
# This extracts the file info from the header
sub handleFind{
# get the file name
my $FullFoundFile = $File::Find::name;
#print $FullFoundFile,"\n";
return if ($opt{x} and $FullFoundFile =~ /$opt{x}/i);
# parse if it ends in ai
findLinkedFiles($FullFoundFile, $File::Find::dir) if ($FullFoundFile =~ /\.ai$/i);
}
# This does the actual parsing of the Illustrator Files
sub findLinkedFiles{
my ($InFile,$InDir)=#_;
# protect with escaped quotes for shell if non-empty
my $ProtectedInDir=$InDir?"\"$InDir\"":$InDir;
die "Can't open $InFile \: $!\n" unless open(AIFILE, "<$InFile");
binmode(AIFILE);
# %%DocumentFiles is the starting point
$/="%%";
my #lines = readline<AIFILE>;
if(#lines==0){
print STDERR "can't read header of $InFile\n" if $opt{v} ; # the header length
return;
}
print "################\n";
if ($opt{s}){
print "# FILE = ",basename($InFile),"\n";
} else {
print "# FILE = ",$InFile,"\n";
}
for my $i ( 0 .. $#lines ){
# if ( $lines[$i]=~/^DocumentFiles\:(.*?)\W+%%/){
# not sure why we need two % signs here
if ( $lines[$i]=~/^DocumentFiles\:(.*?)\W+%/){
print mystatus($1,$InDir)," " if $opt{u} and not $opt{n};
print "\"$1\" ",$opt{d}?$ProtectedInDir:"","\n" unless $opt{n};
$i++;
mysymlink($1,$InDir) if $opt{l};
while($lines[$i]=~/^[+](.*?)\W\%.*$/){
# print "\"$1\" $InDir\n"; $i++;
print mystatus($1,$InDir)," " if $opt{u} and not $opt{n};
print "\"$1\" ",$opt{d}?$ProtectedInDir:"","\n"unless $opt{n};
$i++;
mysymlink($1,$InDir) if $opt{l};
}
}
}
}

Resources