0

I tried to output my file to the specified path with the file name created with current date and time so I first used timestr to store the current date and time:

import time
timestr = time.strftime("%Y%m%d-%H%M%S")

The output directory is defined as below:

FolderLocation = r'C:\Users\Desktop\Tool'

Results= FolderLocation + "\Results -" + timestr + ".xlsx"

with pd.ExcelWriter(Results, engine='openpyxl', mode='a') as writer:

    IR.to_excel(writer, sheet_name='Inputs',startrow=0, index=False)

However, when running the program, the single-backslashes in my specified output directory are changed to double-backslashes and results in the below error:


FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Desktop\\Tool\\Results -20210516-143255.xlsx'

As I would like to save the output files with file name of current date and time, I cannot give the name of a file which actually exists. Does anyone have an idea how to solve this? Many thanks!

  • This is just the way backslashes are represented (escaped with another backslash) when Python prints the representation of the string. There really is only one. Strings are shown this way so that special characters like `\n`, `\t`... can be represented. – Thierry Lathuille May 16 '21 at 10:45
  • So, the problem is elsewhere in the name you passed. Are you sure that the user name isn't missing from your path? – Thierry Lathuille May 16 '21 at 10:48
  • Does this answer your question? [FileNotFoundError: \[Errno 2\] :No such file or directory: 'C:/Users/My\_computer/Desktop/Compare/MHAN-master/AID\_train/AID\_train\_LR/x4\\9.png'](https://stackoverflow.com/questions/67570563/filenotfounderror-errno-2-no-such-file-or-directory-c-users-my-computer-d) – ValentinB May 19 '21 at 13:32

3 Answers3

3

The problem is not that there are 2 backslashes in your path. I think your path is wrong. The FolderLocation variable should probably be something like r'C:\Users\Your_Username\Desktop\Tool'.

The 2 backslashes are only because an r-String converts all \ to \\ when used as normal string. Double backslashes are treated in Python like one backslash.

Max
  • 61
  • 3
0

I think mode='a' in openpyxl doesn't create non-existing files in contrast to how usual file handling in Python works. I tried your code with mode='w' and it does seem to work. If the file is already present, mode='a' works.

And as other answers have mentioned double slashes isn't the problem. You can read more about "Escape sequences in Python" for the same

ram
  • 437
  • 2
  • 12
0

See my answer in: FileNotFoundError: [Errno 2] :No such file or directory: 'C:/Users/My_computer/Desktop/Compare/MHAN-master/AID_train/AID_train_LR/x4\\9.png'

'C:/Users/My_computer/Desktop/Compare/MHAN-master/AID_train/AID_train_LR/x4\9.png'

Your string contains a double backslash at the end of the path, that's why you can't access the directory

use a raw string like

r'yourString' or review your os.path.join

EDIT:

Try to convert every string into a raw-String, like mentioned above. You are still getting double backslashes, because certain \character combinations are escaped.

These are the escaped characters:

Edit your code to:

self.HR =r'C:/Users/My_computer/Desktop/Compare/MHAN- master/AID_train/AID_train_HR' self.LR =r'C:/Users/My_computer/Desktop/Compare/MHAN- master/AID_train/AID_train_LR/x4' Please notice the "r" in front of the string to convert them into raw-Strings.

ValentinB
  • 71
  • 7