Can I somehow test if a file exists? And can I use this to do only things when it exists? I like to do something like

if(exist('members.csv')) {
 do something
  • 7
    See here \IfFileExists{file}{then-code}{else-code} \InputIfFileExists{file}{then-code}{else-code}
    – Eddy_Em
    Commented Feb 14, 2013 at 13:14
  • 2
    An example invokation of \IfFileExists : tex.stackexchange.com/questions/39982/…
    – T. Verron
    Commented Feb 14, 2013 at 13:15
  • 4
    If anything, the other question should be merged into this one.
    – Seamus
    Commented Feb 14, 2013 at 18:16
  • 2
    I don't think this is a duplicate since the linked question is about a more specific problem that happens to have the same solution. In my opinion this question/answer combination is clearer if you don't specifically want a fallback figure to include when a file is not present. Commented May 9, 2020 at 20:17
  • @Circumscribe Reopen it :-)
    – Sebastiano
    Commented May 9, 2020 at 21:55

2 Answers 2


Yes, you can:


Notice that this looks for the file in all search pathes of LaTeX, so not only in the current directory, but in the texmf tree as well. Therefore, you can use it for instance for a "poor man's solution" when a package is missing:


If you really want to search only in the current directory, you can do so by saying

  • 2
    To add to @tohecz answer (I don't believe I can edit yet), I believe that if you want to restrict it to just the current directory, you can use this: \IfFileExists{./filename}{true-branch}{false-branch} and the ./ stops it looking in the other places. Commented Feb 14, 2013 at 17:56
  • 1
    This seems to have problems with underscores in filenames? Commented Oct 14, 2015 at 13:39
  • @fuenfundachtzig That's quite possible, I didn't check.
    – yo'
    Commented Oct 14, 2015 at 13:55
  • 5
    What is the package here for \IfFileExists? Commented Nov 29, 2016 at 12:58
  • 1
    @LéoLéopoldHertz준영 LaTeX kernel. No package necessary. Commented May 27, 2019 at 9:12

The question was not limited to LaTeX here but the answer here is only usable in LaTeX. I add other alternatives.

At TeX primitive level:

\openin15=filename   % or \openin15=./filename for current directory only
\ifeof15           ... file does not exist
\else\closein15    ... file exists

You can allocate the input file descriptor (15 here) by Plain TeX macro \newread. It means tat more common is: \newread\testfile, \openin\tesfile=filename and \ifeof\testfile.

The main point is that TeX allows to do \openin of non-existed file, but it returns true when \ifeof is used before first \read. On the other hand, if the file exists and it is empty, then first \ifeof returns false, first \read defines empty macro and the \ifeof (used after this \read) returns true.

Notice: All TeX engines since 2020 allow alternative syntax \openin15={\filename} which is incompatible with TeX 82 but allows to use spaces in file names.

If you are using OpTeX then there is a macro \isfile for doing this task:

 \isfile{filename}\iffalse ...file doesn't exist...\fi
 \isfile{filename}\iftrue  ...file do exist...\fi
 \isfile{filename}\iftrue  ...file do exist...\else ...file doesn't exist...\fi
  • About the remark, I believe \openin15="file name" also work and allow spaces in file name. Not sure about the compatibility. (that having said it means " cannot be used in file name at all)
    – user202729
    Commented May 13, 2023 at 3:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .