Reputation: 3
I'm learning python and programming and while following a tutorial I ran into an issue that's resulting in an Errno 22. Thank you!
OSError: [Errno 22] Invalid argument: 'logs/2021-05-14 13:56:36.txt'
Here's the code:
def Write_to_file(Date,net_worth,filename='{}.txt'.format(datetime.now().strftime("%Y-
%m-%d %H:%M:%S"))):
for i in net_worth:
Date += " {}".format(i)
#print(Date)
if not os.path.exists('logs'):
os.makedirs('logs')
file = open("logs/"+filename, 'a+')
file.write(Date+"\n")
file.close()
And here's the error and traceback
Traceback (most recent call last):
File "C:\Users\Conor\Documents\GitHub\RL-Bitcoin-trading-bot\RL-Bitcoin-trading-bot_2\RL-
Bitcoin-trading-bot_2.py", line 176, in <module>
Random_games(test_env, visualize=True, train_episodes = 1, training_batch_size=300)
File "C:\Users\Conor\Documents\GitHub\RL-Bitcoin-trading-bot\RL-Bitcoin-trading-bot_2\RL-
Bitcoin-trading-bot_2.py", line 156, in Random_games
state, reward, done = env.step(action)
File "C:\Users\Conor\Documents\GitHub\RL-Bitcoin-trading-bot\RL-Bitcoin-trading-bot_2\RL-
Bitcoin-trading-bot_2.py", line 118, in step
Write_to_file(Date, self.orders_history[-1])
File "C:\Users\Conor\Documents\GitHub\RL-Bitcoin-trading-bot\RL-Bitcoin-trading-
bot_2\utils.py", line 27, in Write_to_file
file = open("logs/"+filename, 'a+')
OSError: [Errno 22] Invalid argument: 'logs/2021-05-14 13:56:36.txt'
[Finished in 2.9s with exit code 1]
[shell_cmd: python -u "C:\Users\Conor\Documents\GitHub\RL-Bitcoin-trading-bot\RL-Bitcoin-
trading-bot_2\RL-Bitcoin-trading-bot_2.py"]
[dir: C:\Users\Conor\Documents\GitHub\RL-Bitcoin-trading-bot\RL-Bitcoin-trading-bot_2]
[path:C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPower Shell\v1.0;C:\WINDOWS\System32\OpenSSH;C:\Program Files\Intel\WiFi\bin;C:\Program Files\Common Files\Intel\WirelessCommon;C:\Program Files (x86)\QuickTime\QTSystem;C:\Program Files (x86)\PuTTY;C:\Program Files\MATLAB\R2020b\bin;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Users\Conor\AppData\Local\Programs\Python\Launcher;C:\Users\Conor\AppData\Local\Pr ograms\Python\Python38-32\Scripts;C:\Users\Conor\AppData\Local\Programs\Python\Python38- 32;C:\Users\Conor\AppData\Local\Microsoft\WindowsApps;C:\Users\Conor\AppData\Local\GitHubDesk top\bin]
Upvotes: 0
Views: 1538
Reputation: 168967
Colons are not allowed in filenames under Windows.
Use another character for separating those time components.
Also, your filename=...
doesn't do what you expect, since parameter default values are evaluated only once, when the module is imported.
You're probably looking for
def Write_to_file(Date, net_worth, filename=None):
if not filename:
filename = "{}.txt".format(datetime.now().strftime("%Y-%m-%d %H-%M-%S"))
Date += " ".join(str(i) for i in net_worth)
os.makedirs("logs", exist_ok=True)
with open("logs/" + filename, "a+") as file:
print(Date, file=file)
Upvotes: 0