title: why the python file handling not recognizing file name?
body: <p>I want to replace a string with another string in a file. I have below program to perform the task.</p>
  <pre><code>import os
  import sys
  import traceback
  from glob import iglob
  
  
  def usage():
      print('Usage: python FindAndReplace.py [Old String] [New String] '
            '[File Filters(default:&quot;.txt,.xml&quot;)] [Directory To Check(.)]')
  
  
  def search_replace_string(fileName, old_str, new_str):
      if not(os.path.isfile(fileName) and os.access(fileName, os.W_OK)):
          print(&quot;Warning: Skipping..File does not exist or and is not writeable:&quot; + filename)
          return False
  
      fileupdated = False
  
      # Read the old file
  
      with open(fileName, 'r') as f:
          newlines = []
          for lines in f.readlines():
              if old_str in lines:
                  fileupdated = True
              line = lines.replace(old_str, new_str)
              newlines.append(line)
  
      # Write changes to same file
  
      if fileupdated:
          print(&quot;string Found and Updating File: &quot; + fileName)
          try:
              with open(fileName, 'w') as f:
                  for line in newlines:
                      f.write(line)
          except:
              print(&quot;Error: Cannot open/access existing file for writing: &quot; + fileName)
      return fileupdated
  
  
  def main():
      try:
          DEFAULT_PATH = iglob(str('&lt;path_to_file.xml'))
          if len(sys.argv) &lt; 3:
              usage()
              # old/new string required parameters, exit if not supplied
              sys.exit(-1)
          else:
              oldString = sys.argv[1]
              newString = sys.argv[2]
  
          if len(sys.argv) &lt; 4:
              patterns = ['.xml', '.txt']
  
          else:
              stringFilter = sys.argv[3]
              patterns = stringFilter.split(',')
  
          if len(sys.argv) &lt; 5:
              path = DEFAULT_PATH
          else:
              path = sys.argv[4]
  
          print('[Old String]    :' + oldString)
          print('[New String]    :' + newString)
          print('[File Filters]    :' + ', '.join(patterns))
          print('[Directory To Check]    :' + path)
  
          if not os.path.exists(path):
              raise Exception(&quot;Selected path does not exist: &quot; + path)
  
  
          # Walk through directory structure looking for files matching patterns
  
          matchingFileList = [os.path.join(dp, f) for dp, dn, filenames in os.walk(path) for f in filenames if os.path.splitext(f)[1] in patterns]
  
          print('Files found matching patterns: ' + str(len(matchingFileList)))
          filecount = 0
          filesReplaced = 0
  
          for currFile in matchingFileList:
              filecount += 1
              filesReplaced = search_replace_string(currFile, old_str, new_str)
              if filesReplaced:
                  filesReplaced += 1
  
          print(&quot;Total Files Searched   :&quot; + str(filecount))
          print(&quot;Total Files Replaced/Updated  :&quot; + str(filesReplaced))
      except Exception as err:
          print(traceback.format_exception_only(type(err), err)[0].rstrip())
          sys.exit(-1)
  
  
  if __name__ == '__main__':
      main()
  </code></pre>
  <p>When I am executing it from a command line I am getting below error:</p>
  <pre><code>(null): can't open file 'uro.py': [Errno 2] No such file or directory
  </code></pre>
  <p>Below is the command line argument I am giving</p>
  <pre><code>python uro.py &lt;file_path&gt; &lt;old_str&gt; &lt;new_str&gt;
  </code></pre>
  <p>NOTE: I am using xml file.
  I wanted to develop a logic that will take file_name, old and new string as a command line argument. In the error one can see that the program is considering the python file a input file. While it should take the path of the file that I give with CMD argument.</p>
  <p>What's the mistake here? Please suggest. Thank you</p>