Note that there are some explanatory texts on larger screens.

plurals
  1. PORead filenames from a textfile in python (double backslash issue)
    primarykey
    data
    text
    <p>I am trying to read a list of files from a text file. I am using the following code to do that:</p> <pre><code>filelist = input("Please Enter the filelist: ") flist = open (os.path.normpath(filelist),"r") fname = [] for curline in flist: # check if its a coment - do comment parsing in this if block if curline.startswith('#'): continue fname.append(os.path.normpath(curline)); flist.close() #close the list file # read the slave files 100MB at a time to generate stokes vectors tmp = fname[0].rstrip() t = np.fromfile(tmp,dtype='float',count=100*1000) </code></pre> <p>This works perfectly fine and I get the following array:</p> <pre><code>'H:\\Shaunak\\TerraSAR_X- Sep2012-Glacier_Velocity_Gangotri\\NEST_oregistration\\Glacier_coreg_Cnv\\i_HH_mst_08Oct2012.bin\n' 'H:\\Shaunak\\TerraSAR_X- Sep2012-Glacier_Velocity_Gangotri\\NEST_oregistration\\Glacier_coreg_Cnv\\i_HH_mst_08Oct2012.bin\n' 'H:\\Shaunak\\TerraSAR_X- Sep2012-Glacier_Velocity_Gangotri\\NEST_oregistration\\Glacier_coreg_Cnv\\q_HH_slv3_08Oct2012.bin\n' 'H:\\Shaunak\\TerraSAR_X- Sep2012-Glacier_Velocity_Gangotri\\NEST_oregistration\\Glacier_coreg_Cnv\\q_VV_slv3_08Oct2012.bin' </code></pre> <p>The problem is that the <code>'\'</code> charecter is escaped and there is a trailing <code>'\n'</code> in the strings. I used the <code>str.rstrip()</code> to get rid of the '\n' - this works, but leaves the problem of the two back slashes.</p> <p>I have used the following approaches to try getting rid of these:</p> <ol> <li><p>Used the <code>codecs.unicode_escape_decode()</code> but I get this error: <code>UnicodeDecodeError: 'unicodeescape' codec can't decode bytes in position 56-57: malformed \N character escape</code>. Clearly this is not the right approach because I just want to decode the backslashed, not the rest of the string.</p></li> <li><p>This does not work either: <code>tmp = fname[0].rstrip().replace(r'\\','\\');</code></p></li> </ol> <p>Is there no way to make readline() read a raw string?</p> <hr> <p>UPDATE:</p> <p>Basically I have a text file with 4 file names I would like to open and read data from in python. The text file contains:</p> <pre><code>H:\Shaunak\TerraSAR_X-Sep2012-Glacier_Velocity_Gangotri\NEST_oregistration\Glacier_coreg_Cnv\i_HH_mst_08Oct2012.bin H:\Shaunak\TerraSAR_X-Sep2012-Glacier_Velocity_Gangotri\NEST_oregistration\Glacier_coreg_Cnv\i_HH_mst_08Oct2012.bin H:\Shaunak\TerraSAR_X-Sep2012-Glacier_Velocity_Gangotri\NEST_oregistration\Glacier_coreg_Cnv\q_HH_slv3_08Oct2012.bin H:\Shaunak\TerraSAR_X-Sep2012-Glacier_Velocity_Gangotri\NEST_oregistration\Glacier_coreg_Cnv\q_VV_slv3_08Oct2012.bin </code></pre> <p>I would like to open each file one by one and read 100MBs of data from them. When I use this command:<code>np.fromfile(flist[0],dtype='float',count=100)</code> I get this error:</p> <pre><code>Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; FileNotFoundError: [Errno 2] No such file or directory: 'H:\\Shaunak\\TerraSAR_X-Sep2012-Glacier_Velocity_Gangotri\\NEST_oregistration\\Glacier_coreg_Cnv\\i_HH_mst_08Oct2012.bin' </code></pre> <hr> <p>Update</p> <p>Full Traceback:</p> <pre><code>Please Enter the filelist: H:/Shaunak/TerraSAR_X- Sep2012-Glacier_Velocity_Gangotri/NEST_oregistration/Glacier_coreg_Cnv/filelist.txt Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; File "G:\WinPython-32bit-3.3.2.3\python-3.3.2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 581, in runfile execfile(filename, namespace) File "G:\WinPython-32bit-3.3.2.3\python-3.3.2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 41, in execfile exec(compile(open(filename).read(), filename, 'exec'), namespace) File "H:/Shaunak/Programs/Arnab_glacier_vel/Stokes_generation_2.py", line 28, in &lt;module&gt; t = np.fromfile(tmp,dtype='float',count=100*1000) FileNotFoundError: [Errno 2] No such file or directory: 'H:\\Shaunak\\TerraSAR_X-Sep2012-Glacier_Velocity_Gangotri\\NEST_oregistration\\Glacier_coreg_Cnv\\i_HH_mst_08Oct2012.bin' &gt;&gt;&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload