Python 'Create File' Error

Asked 2 weeks ago, Updated 2 weeks ago, 1 views

When creating a file (.xlsx), it works properly on the vscode program, but when you run it through an exe file, the file is not created and an error occurs. I changed the folder permission to everyone because I thought it was a folder permission problem, but the error was not resolved. Also, I thought it was a path problem, so I set the path as an absolute path, but the .txt file is generated normally.The xlsx file is not created.I have no idea why.

import openpyxl
import os

from openpyxl import Workbook

dir = r"C:\Users\82107\Desktop\TEST"
dir = os.path.abspath(dir)
os.chdir(dir)
print("current dir:" + os.getcwd())

f = open(os.path.join(dir, 'test.txt'), "a", encoding='utf-8')
f.write ("Test.")
f.close()
print ("txt successful")
print("File creation location: " + os.path.abspath('test.txt'))

print("current dir:" + os.getcwd())
wb = Workbook()
wb.save(os.path.join(dir, 'test.xlsx'))
print ("xlsx successful")
print("File creation location: " + os.path.abspath('test.xlsx'))

python openpyxl

2022-09-20 08:41

2 Answers

That's weird.

First of all, I tested it on my system with the code you asked me (only changing the username, creating the TEST directory), and in both cases it worked.

If I were you, I'd leave logging with the procmon tool to see which file path you're actually trying to access. This could be a little hard.

It's a bit of a turnaround, but what I can do is

The exe file generated by the questioner was uploaded and the file execution test was conducted.


2022-09-20 08:41

I left a log and checked it with the procmon program that daewon mentioned. The following logs were left for the test.xlsx file:


2022-09-20 08:41

If you have any answers or tips


© 2022 pinfo. All rights reserved.