3

I am on Windows, and none of the files exist in my directory.

I have trouble figuring out why:

fid = open('L01A.txt', 'x')
fid.write('A') 
fid.close()

fid = open('L01a.txt', 'x')
fid.write('a')
fid.close()

gives me:

[Errno 17] File exists: 'L01a.txt'.

Lalush
  • 41
  • 5

1 Answers1

5

You open your file with mode 'x', which is used only to create a file. From the doc

'x', open for exclusive creation, failing if the file already exists

You should use another mode, here is a useful link to descriptions to different modes that can be useful to you

python open built-in function: difference between modes a, a+, w, w+, and r+?

Edit: Apparently your error is that you cannot create 2 files with names L01A and L01a with two different cases, this is windows file system is not case sensitive. You cannot create two distincts files.

If you absolutely need the case sensitive, you can enable NTFS to do so in the directory, with launching an admin powershell and execute fsutil.exe file setCaseSensitiveInfo C:\folder enable

According to this thread, you might want to enable this for all subdirectories, here is a way to do so Apply setCaseSensitiveInfo recursively to all folders and subfolders.

Thanks Lalush for the thread.

BlueSheepToken
  • 5,751
  • 3
  • 17
  • 42
  • The 'x' was only added to show a distinct error. If i use 'w' the file 'L01A.txt' with the content 'A' will change its content to 'a' and not create a second file called 'L01a.txt'. I want to create a second file, but the difference between A and a is not recognized. – Lalush Feb 26 '19 at 10:02
  • 1
    @Lalush, Updated my answer, if you are on windows you should enable case sensitive – BlueSheepToken Feb 26 '19 at 10:12
  • 1
    you are right I am on Windows. Just enabled case sensitivity and it works. Thanks a lot! – Lalush Feb 26 '19 at 10:21
  • @Lalush I am glad to help :) – BlueSheepToken Feb 26 '19 at 10:29
  • already tried the fsutil.exe command in the command shell and it worked that way. The actual issue is that it only enables case sensitivity for that directory and not the subsequent subfolders, which I really want. I am not the only one with that issue and it seems to be a common Windows problem. In another thread this problem seemed to be solved through an additional power shell script https://stackoverflow.com/questions/51591091/apply-setcasesensitiveinfo-recursively-to-all-folders-and-subfolders – Lalush Feb 26 '19 at 11:23