简体   繁体   中英

File not found with Python's fileinput

Given the following code:

# Edit build number in test report
print(path)  # TODO remove
html_report = fileinput.input(path, inplace=True)
for line in html_report:
    print(line.replace('$BUILD_NUMBER',
                       args.number).rstrip())
html_report.close()

I get the following output:

/home/jenkins/workspace/reports/report201610261053.html
Traceback (most recent call last):
  File "report_generator.py", line 58, in <module>
    for line in html_report:
  File "/usr/lib/python2.7/fileinput.py", line 252, in next
    line = self.readline()
  File "/usr/lib/python2.7/fileinput.py", line 321, in readline
    os.rename(self._filename, self._backupfilename)
OSError: [Errno 2] No such file or directory

If I just use the command:

gedit /home/jenkins/workspace/reports/report201610261053.html

I can check that the file exists. In fact, if it didn't I would expect this error to be raised in the fileinput.input() line, not in the line loop.

Any idea of what's wrong?

What is your "path" value? I think you should try to use absolute path

You can also check the user permissions to file.

I don't see anything wrong in the code that you have shown.

I can check that the file exists. In fact, if it didn't I would expect this error to be raised in the fileinput.input() line, not in the line loop.

The error is reported only when an attempt is made to open file and it happens in the for loop.

Is it possible that your code is running under a different user and doesn't see the file on that path as compared to you manually verifying file existence?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM