0

I have a file on a memory stick which i want to use in my python script. The easiest way to do this would be to move the file into the directory it currently looks in but for security reasons i cant do this. How do i write the file location so that it successfully finds the file on the memory stick and reads it in? this is what ive tried...

import asciitable
import numpy as np
import pylab as plt

x=asciitable.read('E:/ECBGF/bg0809_protected.txt', guess=False,delimiter='\t',fill_values=[('', '-999')])
10
  • 1
    if you use linux, you can just do a symbolic link to the directory of your choice. Commented Aug 19, 2013 at 15:15
  • im using windows but the python program im using runs through a linux server. How would i do the symbolic link? Commented Aug 19, 2013 at 15:17
  • have a look at "man ln" Commented Aug 19, 2013 at 15:19
  • In Linux you can read from memory stick as from regular disk. For example ubuntu automount your usb storages to /media directory with it's id. Commented Aug 19, 2013 at 15:19
  • What's wrong with open('/path/to/file')? Commented Aug 19, 2013 at 15:22

1 Answer 1

1

Since you don't explicitly open the file yourself, the simplest thing to do in this case would be to just make sure that the path to the file you pass asciitable.read() is valid. Here's what I mean:

import asciitable
import os
from string import ascii_uppercase
import sys

PATH_TEMPLATE = '{}:/ECBGF/bg0809_protected.txt'
for drive in ascii_uppercase[:-24:-1]: # letters 'Z' down to 'D'
    file_path = PATH_TEMPLATE.format(drive)
    if os.path.exists(file_path):
        break
else:
    print 'error, file not found'
    sys.exit(1)

x = asciitable.read(file_path, guess=False, delimiter='\t',
                    fill_values=[('', '-999')])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.