1

It looks stupid but I can't handle this issue.

cwd = os.getcwd()
os.chdir(cwd)
file_name = 'tes.txt'
with open(file_name, 'w+') as f:
    f.write('hhhh')
f.close()

when I start this simple code it always returns me

with open(file_name, 'w+') as f:

FileNotFoundError: [Errno 2] No such file or directory: 'test.txt'

On other computer the same code works well, as expected.

If I'm trying to address to file which already exists

cwd = os.getcwd()
os.chdir(cwd)
# print(os.listdir(cwd))
file_name = 'tether_full_data.txt'
with open(file_name, 'w+') as f:
    f.write('hhhh')
f.close()

it returns

OSError: [Errno 9] Bad file descriptor

I did a lot of search and tried a lot of ways. But with no success on this PC (only on this PC).

Windows 10 PRO, version 1709, OS Build 16299.19

bastelflp
  • 9,362
  • 7
  • 32
  • 67
Sergey Solod
  • 695
  • 7
  • 15
  • That does not solve your problem, but you don't need the `f.close()` when you are using a context manager (`with`) to open the file. – bastelflp Nov 15 '17 at 20:57
  • And if you just want to write to the file, use only `w` instead of `w+`, see https://stackoverflow.com/a/16212401/5276734 for all file modes. – bastelflp Nov 15 '17 at 21:02
  • And your first error message mentions another file then your code. – bastelflp Nov 15 '17 at 21:03
  • 1
    You will get the second error [Errno 9] if a Python file was closed from "the outside", i.e. not from the file object's close() method. As for it not working, did you try giving absolute path of the file? – d_void Nov 15 '17 at 22:42
  • @d_void cwd = os.getcwd() os.chdir(cwd) print(cwd) # print(os.listdir(cwd)) file_name = r'C:\Users\sergeysolod\OneDrive\ICO\tether_full_data.txt' with open(file_name, 'w') as f: f.write('hhhh') f.close() It returns the same OSError: [Errno 9] Bad file descriptor – Sergey Solod Nov 16 '17 at 06:05
  • 1
    @bastelflp Thanks for your willing to help! Actually I have been trying a lot of different options, 'w', 'w+', 'r', 'a'. Two different names of files mean that I tried to use two files to test. One of those tests was to try to create a new file. Next attempt was to write data (simple string) to file which already exists. \ – Sergey Solod Nov 16 '17 at 06:13

0 Answers0