Let me just start by pointing out I'm a new StackExchange user as well as a new Mathematica user (started today!) I mention this as a context to my ineptitude :)

I am attempting to read through a list with the names of files I want to open, import data from them and plot that data. Here is the code I'm trying to use:

testfiles = ({"aspirina.txt", "silicio.txt", "silicio20.txt", 
   "silicio20luz.txt", "tio2.txt", "vidro.txt", "vidro2.txt"})

Do[{data = Import[path <> ToString[i, StandardForm], {"Table"}], 
  Print[path <> ToString[i, StandardForm]], 
  Print[ListLinePlot[data]]}, {i, testfiles}]

Out[500]= {aspirina.txt, silicio.txt, silicio20.txt, silicio20luz.txt,
  tio2.txt, vidro.txt, vidro2.txt}
Import::nffil: File not found during Import. >> 
~/Desktop/TCOEM/Espetros Raman/aspirina.txt


The directory and file names are correct, so much so that when I try this directly by just typing the exact same directory I get the desired result.

Firstly, I changed the FormatType setting of ToString[] to StandardForm because the default (OutputForm) would give me an extra whitespace on either side of the full stop in the file names. Why does it do this?

Secondly, why is it that even when Print[] seems to indicate the directory is correct I get the exception saying the file cannot be found during Import?

I specifically did not format the names of the files in the testfiles list individually as strings (using "" around each one) to allow for the case where I have hundreds of such files and just want to copy and paste their names via whatever file manager I happen to be using.

Any help will be greatly appreciated!

  • $\begingroup$ It's usually easier to let Mathematica find the files, as in VLC's answer, but if you have to paste filenames from the clipboard you can paste between two quote marks to get a single long string, and use StringSplit to break it into individual filenames. $\endgroup$ Oct 26, 2012 at 14:09
  • $\begingroup$ Good to know thanks :) $\endgroup$
    – wrighteap
    Oct 26, 2012 at 19:30

2 Answers 2


Filenames are always strings. What you do when you use something like aspirina.txt without quotes is, you use the Dot operator (which is . in infix) to and give the two variables aspirina and txt as input.

Please try

testfiles = {"aspirina.txt", "silicio.txt", "silicio20.txt", "silicio20luz.txt", 
"tio2.txt", "vidro.txt", "vidro2.txt"};

Do[{data = Import[path <> i, {"Table"}], 
  Print[path <> i], 
  Print[ListLinePlot[data]]}, {i, testfiles}]
  • $\begingroup$ I had not realised Dot existed and that it is equivalent to . Thank you. $\endgroup$
    – wrighteap
    Oct 26, 2012 at 19:24

You don't need to copy and paste the names of your files. If all the files you want to import are in a specific directory you can just import them directly from that directory.

You first need to define where your directory is by using SetDirectory. Then you can get the list of your files:

filesToImport = ToFileName[Directory[], #] & /@ FileNames["*.txt"]

This line will give you a list of all the paths to the files within the directory.

  • $\begingroup$ This appears to be an altogether more sensible approach. Thanks! $\endgroup$
    – wrighteap
    Oct 26, 2012 at 19:27

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.