Python shows No such file or directory Error though the file exists












2















I want to search a string from multiple files



What I tried:



import os
path= 'sample1/nvram2/logs'
all_files=os.listdir(path)
for my_file1 in all_files:
print(my_file1)
with open(my_file1, 'r') as my_file2:
print(my_file2)
for line in my_file2:
if 'string' in line:
print(my_file2)


Output:



C:Usersuser1scripts>python search_string_3.py
abcd.txt
Traceback (most recent call last):
File "search_string_3.py", line 6, in <module>
with open(my_file1, 'r') as my_file2:
FileNotFoundError: [Errno 2] No such file or directory: 'abcd.txt'


But file abcd.txt is present in C:Usersuser1scriptssample1nvram2logs



Why the Error shows that No such file or directory?



Using glob:



The following error was displayed when I use all_files=glob.glob(path) instead of all_files=os.listdir(path)



C:Usersuser1scripts>python search_string_3.py
sample1/nvram2/logs
Traceback (most recent call last):
File "search_string_3.py", line 7, in <module>
with open(my_file1, 'r') as my_file2:
PermissionError: [Errno 13] Permission denied: 'sample1/nvram2/logs'









share|improve this question



























    2















    I want to search a string from multiple files



    What I tried:



    import os
    path= 'sample1/nvram2/logs'
    all_files=os.listdir(path)
    for my_file1 in all_files:
    print(my_file1)
    with open(my_file1, 'r') as my_file2:
    print(my_file2)
    for line in my_file2:
    if 'string' in line:
    print(my_file2)


    Output:



    C:Usersuser1scripts>python search_string_3.py
    abcd.txt
    Traceback (most recent call last):
    File "search_string_3.py", line 6, in <module>
    with open(my_file1, 'r') as my_file2:
    FileNotFoundError: [Errno 2] No such file or directory: 'abcd.txt'


    But file abcd.txt is present in C:Usersuser1scriptssample1nvram2logs



    Why the Error shows that No such file or directory?



    Using glob:



    The following error was displayed when I use all_files=glob.glob(path) instead of all_files=os.listdir(path)



    C:Usersuser1scripts>python search_string_3.py
    sample1/nvram2/logs
    Traceback (most recent call last):
    File "search_string_3.py", line 7, in <module>
    with open(my_file1, 'r') as my_file2:
    PermissionError: [Errno 13] Permission denied: 'sample1/nvram2/logs'









    share|improve this question

























      2












      2








      2








      I want to search a string from multiple files



      What I tried:



      import os
      path= 'sample1/nvram2/logs'
      all_files=os.listdir(path)
      for my_file1 in all_files:
      print(my_file1)
      with open(my_file1, 'r') as my_file2:
      print(my_file2)
      for line in my_file2:
      if 'string' in line:
      print(my_file2)


      Output:



      C:Usersuser1scripts>python search_string_3.py
      abcd.txt
      Traceback (most recent call last):
      File "search_string_3.py", line 6, in <module>
      with open(my_file1, 'r') as my_file2:
      FileNotFoundError: [Errno 2] No such file or directory: 'abcd.txt'


      But file abcd.txt is present in C:Usersuser1scriptssample1nvram2logs



      Why the Error shows that No such file or directory?



      Using glob:



      The following error was displayed when I use all_files=glob.glob(path) instead of all_files=os.listdir(path)



      C:Usersuser1scripts>python search_string_3.py
      sample1/nvram2/logs
      Traceback (most recent call last):
      File "search_string_3.py", line 7, in <module>
      with open(my_file1, 'r') as my_file2:
      PermissionError: [Errno 13] Permission denied: 'sample1/nvram2/logs'









      share|improve this question














      I want to search a string from multiple files



      What I tried:



      import os
      path= 'sample1/nvram2/logs'
      all_files=os.listdir(path)
      for my_file1 in all_files:
      print(my_file1)
      with open(my_file1, 'r') as my_file2:
      print(my_file2)
      for line in my_file2:
      if 'string' in line:
      print(my_file2)


      Output:



      C:Usersuser1scripts>python search_string_3.py
      abcd.txt
      Traceback (most recent call last):
      File "search_string_3.py", line 6, in <module>
      with open(my_file1, 'r') as my_file2:
      FileNotFoundError: [Errno 2] No such file or directory: 'abcd.txt'


      But file abcd.txt is present in C:Usersuser1scriptssample1nvram2logs



      Why the Error shows that No such file or directory?



      Using glob:



      The following error was displayed when I use all_files=glob.glob(path) instead of all_files=os.listdir(path)



      C:Usersuser1scripts>python search_string_3.py
      sample1/nvram2/logs
      Traceback (most recent call last):
      File "search_string_3.py", line 7, in <module>
      with open(my_file1, 'r') as my_file2:
      PermissionError: [Errno 13] Permission denied: 'sample1/nvram2/logs'






      python python-3.x






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 9:11









      Dipankar NaluiDipankar Nalui

      2321517




      2321517
























          2 Answers
          2






          active

          oldest

          votes


















          1














          you figured out/guessed the first issue. Joining the directory with the filename solves it. A classic:



          with open(os.path.join(path,my_file1), 'r') as my_file2:


          I wouldn't have cared to answer if you didn't attempted something with glob. Now:



          for x in glob.glob(path):


          since path is a directory, glob evaluates it as itself (you get a list with one element: [path]). You need to add a wildcard:



          for x in glob.glob(os.path.join(path,"*")):


          The other issue with glob is that if the directory (or the pattern) doesn't match anything you're not getting any error. It just does nothing... The os.listdir version crashes at least.



          and also test if it's a file before opening (in both cases) because attempting to open a directory results in an I/O exception:



          if os.path.isfile(x):
          with open ...


          In a nutshell os.path package is your friend when manipulating files.






          share|improve this answer


























          • This solution solved the problem.

            – Dipankar Nalui
            Nov 14 '18 at 10:54



















          -1














          Since the file abcd.txt is present in C:Usersuser1scriptssample1nvram2logs, and the said path is not your working directory, you have to add the same to sys.path



          import os, sys
          path= 'sample1/nvram2/logs'
          sys.path.append(path)


          all_files=os.listdir(path)
          for my_file1 in all_files:
          print(my_file1)
          with open(my_file1, 'r') as my_file2:
          print(my_file2)
          for line in my_file2:
          if 'string' in line:
          print(my_file2)





          share|improve this answer
























          • no, sys.path is for module loading.

            – Jean-François Fabre
            Nov 14 '18 at 9:25











          • sys.path is not only for module loading. Even when you have to import files without giving their complete path, you can append their path using sys.path and then call it directly. It only makes your files searchable by the current path.

            – Pradip Gupta
            Nov 14 '18 at 9:54











          • open doesn't use sys.path at all

            – Jean-François Fabre
            Nov 14 '18 at 10:17











          • I agree with that. However, in general, python would search for the file in all the paths found in sys.path. For example, when you have the file in your cwd, you do not give the absolute path with open as the file is already present in the path for you. Same is the logic with sys.path

            – Pradip Gupta
            Nov 14 '18 at 10:20











          • modules aren't data files.

            – Jean-François Fabre
            Nov 14 '18 at 10:21











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296542%2fpython-shows-no-such-file-or-directory-error-though-the-file-exists%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          you figured out/guessed the first issue. Joining the directory with the filename solves it. A classic:



          with open(os.path.join(path,my_file1), 'r') as my_file2:


          I wouldn't have cared to answer if you didn't attempted something with glob. Now:



          for x in glob.glob(path):


          since path is a directory, glob evaluates it as itself (you get a list with one element: [path]). You need to add a wildcard:



          for x in glob.glob(os.path.join(path,"*")):


          The other issue with glob is that if the directory (or the pattern) doesn't match anything you're not getting any error. It just does nothing... The os.listdir version crashes at least.



          and also test if it's a file before opening (in both cases) because attempting to open a directory results in an I/O exception:



          if os.path.isfile(x):
          with open ...


          In a nutshell os.path package is your friend when manipulating files.






          share|improve this answer


























          • This solution solved the problem.

            – Dipankar Nalui
            Nov 14 '18 at 10:54
















          1














          you figured out/guessed the first issue. Joining the directory with the filename solves it. A classic:



          with open(os.path.join(path,my_file1), 'r') as my_file2:


          I wouldn't have cared to answer if you didn't attempted something with glob. Now:



          for x in glob.glob(path):


          since path is a directory, glob evaluates it as itself (you get a list with one element: [path]). You need to add a wildcard:



          for x in glob.glob(os.path.join(path,"*")):


          The other issue with glob is that if the directory (or the pattern) doesn't match anything you're not getting any error. It just does nothing... The os.listdir version crashes at least.



          and also test if it's a file before opening (in both cases) because attempting to open a directory results in an I/O exception:



          if os.path.isfile(x):
          with open ...


          In a nutshell os.path package is your friend when manipulating files.






          share|improve this answer


























          • This solution solved the problem.

            – Dipankar Nalui
            Nov 14 '18 at 10:54














          1












          1








          1







          you figured out/guessed the first issue. Joining the directory with the filename solves it. A classic:



          with open(os.path.join(path,my_file1), 'r') as my_file2:


          I wouldn't have cared to answer if you didn't attempted something with glob. Now:



          for x in glob.glob(path):


          since path is a directory, glob evaluates it as itself (you get a list with one element: [path]). You need to add a wildcard:



          for x in glob.glob(os.path.join(path,"*")):


          The other issue with glob is that if the directory (or the pattern) doesn't match anything you're not getting any error. It just does nothing... The os.listdir version crashes at least.



          and also test if it's a file before opening (in both cases) because attempting to open a directory results in an I/O exception:



          if os.path.isfile(x):
          with open ...


          In a nutshell os.path package is your friend when manipulating files.






          share|improve this answer















          you figured out/guessed the first issue. Joining the directory with the filename solves it. A classic:



          with open(os.path.join(path,my_file1), 'r') as my_file2:


          I wouldn't have cared to answer if you didn't attempted something with glob. Now:



          for x in glob.glob(path):


          since path is a directory, glob evaluates it as itself (you get a list with one element: [path]). You need to add a wildcard:



          for x in glob.glob(os.path.join(path,"*")):


          The other issue with glob is that if the directory (or the pattern) doesn't match anything you're not getting any error. It just does nothing... The os.listdir version crashes at least.



          and also test if it's a file before opening (in both cases) because attempting to open a directory results in an I/O exception:



          if os.path.isfile(x):
          with open ...


          In a nutshell os.path package is your friend when manipulating files.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 9:46

























          answered Nov 14 '18 at 9:18









          Jean-François FabreJean-François Fabre

          104k955112




          104k955112













          • This solution solved the problem.

            – Dipankar Nalui
            Nov 14 '18 at 10:54



















          • This solution solved the problem.

            – Dipankar Nalui
            Nov 14 '18 at 10:54

















          This solution solved the problem.

          – Dipankar Nalui
          Nov 14 '18 at 10:54





          This solution solved the problem.

          – Dipankar Nalui
          Nov 14 '18 at 10:54













          -1














          Since the file abcd.txt is present in C:Usersuser1scriptssample1nvram2logs, and the said path is not your working directory, you have to add the same to sys.path



          import os, sys
          path= 'sample1/nvram2/logs'
          sys.path.append(path)


          all_files=os.listdir(path)
          for my_file1 in all_files:
          print(my_file1)
          with open(my_file1, 'r') as my_file2:
          print(my_file2)
          for line in my_file2:
          if 'string' in line:
          print(my_file2)





          share|improve this answer
























          • no, sys.path is for module loading.

            – Jean-François Fabre
            Nov 14 '18 at 9:25











          • sys.path is not only for module loading. Even when you have to import files without giving their complete path, you can append their path using sys.path and then call it directly. It only makes your files searchable by the current path.

            – Pradip Gupta
            Nov 14 '18 at 9:54











          • open doesn't use sys.path at all

            – Jean-François Fabre
            Nov 14 '18 at 10:17











          • I agree with that. However, in general, python would search for the file in all the paths found in sys.path. For example, when you have the file in your cwd, you do not give the absolute path with open as the file is already present in the path for you. Same is the logic with sys.path

            – Pradip Gupta
            Nov 14 '18 at 10:20











          • modules aren't data files.

            – Jean-François Fabre
            Nov 14 '18 at 10:21
















          -1














          Since the file abcd.txt is present in C:Usersuser1scriptssample1nvram2logs, and the said path is not your working directory, you have to add the same to sys.path



          import os, sys
          path= 'sample1/nvram2/logs'
          sys.path.append(path)


          all_files=os.listdir(path)
          for my_file1 in all_files:
          print(my_file1)
          with open(my_file1, 'r') as my_file2:
          print(my_file2)
          for line in my_file2:
          if 'string' in line:
          print(my_file2)





          share|improve this answer
























          • no, sys.path is for module loading.

            – Jean-François Fabre
            Nov 14 '18 at 9:25











          • sys.path is not only for module loading. Even when you have to import files without giving their complete path, you can append their path using sys.path and then call it directly. It only makes your files searchable by the current path.

            – Pradip Gupta
            Nov 14 '18 at 9:54











          • open doesn't use sys.path at all

            – Jean-François Fabre
            Nov 14 '18 at 10:17











          • I agree with that. However, in general, python would search for the file in all the paths found in sys.path. For example, when you have the file in your cwd, you do not give the absolute path with open as the file is already present in the path for you. Same is the logic with sys.path

            – Pradip Gupta
            Nov 14 '18 at 10:20











          • modules aren't data files.

            – Jean-François Fabre
            Nov 14 '18 at 10:21














          -1












          -1








          -1







          Since the file abcd.txt is present in C:Usersuser1scriptssample1nvram2logs, and the said path is not your working directory, you have to add the same to sys.path



          import os, sys
          path= 'sample1/nvram2/logs'
          sys.path.append(path)


          all_files=os.listdir(path)
          for my_file1 in all_files:
          print(my_file1)
          with open(my_file1, 'r') as my_file2:
          print(my_file2)
          for line in my_file2:
          if 'string' in line:
          print(my_file2)





          share|improve this answer













          Since the file abcd.txt is present in C:Usersuser1scriptssample1nvram2logs, and the said path is not your working directory, you have to add the same to sys.path



          import os, sys
          path= 'sample1/nvram2/logs'
          sys.path.append(path)


          all_files=os.listdir(path)
          for my_file1 in all_files:
          print(my_file1)
          with open(my_file1, 'r') as my_file2:
          print(my_file2)
          for line in my_file2:
          if 'string' in line:
          print(my_file2)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 9:23









          Pradip GuptaPradip Gupta

          6910




          6910













          • no, sys.path is for module loading.

            – Jean-François Fabre
            Nov 14 '18 at 9:25











          • sys.path is not only for module loading. Even when you have to import files without giving their complete path, you can append their path using sys.path and then call it directly. It only makes your files searchable by the current path.

            – Pradip Gupta
            Nov 14 '18 at 9:54











          • open doesn't use sys.path at all

            – Jean-François Fabre
            Nov 14 '18 at 10:17











          • I agree with that. However, in general, python would search for the file in all the paths found in sys.path. For example, when you have the file in your cwd, you do not give the absolute path with open as the file is already present in the path for you. Same is the logic with sys.path

            – Pradip Gupta
            Nov 14 '18 at 10:20











          • modules aren't data files.

            – Jean-François Fabre
            Nov 14 '18 at 10:21



















          • no, sys.path is for module loading.

            – Jean-François Fabre
            Nov 14 '18 at 9:25











          • sys.path is not only for module loading. Even when you have to import files without giving their complete path, you can append their path using sys.path and then call it directly. It only makes your files searchable by the current path.

            – Pradip Gupta
            Nov 14 '18 at 9:54











          • open doesn't use sys.path at all

            – Jean-François Fabre
            Nov 14 '18 at 10:17











          • I agree with that. However, in general, python would search for the file in all the paths found in sys.path. For example, when you have the file in your cwd, you do not give the absolute path with open as the file is already present in the path for you. Same is the logic with sys.path

            – Pradip Gupta
            Nov 14 '18 at 10:20











          • modules aren't data files.

            – Jean-François Fabre
            Nov 14 '18 at 10:21

















          no, sys.path is for module loading.

          – Jean-François Fabre
          Nov 14 '18 at 9:25





          no, sys.path is for module loading.

          – Jean-François Fabre
          Nov 14 '18 at 9:25













          sys.path is not only for module loading. Even when you have to import files without giving their complete path, you can append their path using sys.path and then call it directly. It only makes your files searchable by the current path.

          – Pradip Gupta
          Nov 14 '18 at 9:54





          sys.path is not only for module loading. Even when you have to import files without giving their complete path, you can append their path using sys.path and then call it directly. It only makes your files searchable by the current path.

          – Pradip Gupta
          Nov 14 '18 at 9:54













          open doesn't use sys.path at all

          – Jean-François Fabre
          Nov 14 '18 at 10:17





          open doesn't use sys.path at all

          – Jean-François Fabre
          Nov 14 '18 at 10:17













          I agree with that. However, in general, python would search for the file in all the paths found in sys.path. For example, when you have the file in your cwd, you do not give the absolute path with open as the file is already present in the path for you. Same is the logic with sys.path

          – Pradip Gupta
          Nov 14 '18 at 10:20





          I agree with that. However, in general, python would search for the file in all the paths found in sys.path. For example, when you have the file in your cwd, you do not give the absolute path with open as the file is already present in the path for you. Same is the logic with sys.path

          – Pradip Gupta
          Nov 14 '18 at 10:20













          modules aren't data files.

          – Jean-François Fabre
          Nov 14 '18 at 10:21





          modules aren't data files.

          – Jean-François Fabre
          Nov 14 '18 at 10:21


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296542%2fpython-shows-no-such-file-or-directory-error-though-the-file-exists%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Florida Star v. B. J. F.

          Danny Elfman

          Lugert, Oklahoma