How to get a file on a memory stick read into a python script?

3.1k Views Asked by At

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')])
1

There are 1 best solutions below

0
On

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')])