0

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]

Conor
  • 3
  • 2
  • Does this answer your question? [OSError \[Errno 22\] invalid argument when use open() in Python](https://stackoverflow.com/questions/25584124/oserror-errno-22-invalid-argument-when-use-open-in-python) – AMC May 14 '21 at 15:34

1 Answers1

0

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)
AKX
  • 152,115
  • 15
  • 115
  • 172
  • Thank you again, the problem was definitely with the colons, it is writing to files now. The only issue is that it's now splitting each log entry into different files, as opposed to creating one file. – Conor May 14 '21 at 13:59
  • You might want to change the filename to not include seconds, then... – AKX May 14 '21 at 14:40
  • Why use `print()` instead of `file.write()` ? – AMC May 14 '21 at 15:35
  • @amc It deals with the newline by itself. – AKX May 14 '21 at 17:34
  • Ah that makes sense. Wouldn't it be safer/clearer to do `if filename is None:` rather than `if not filename:` ? – AMC May 14 '21 at 20:09
  • @amc An empty filename wouldn't be valid either. It'd be cleaner and purer, but `if not` works well here. – AKX May 15 '21 at 08:57