Python Forum
[SOLVED] OSError: [Errno 22] Invalid argument
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] OSError: [Errno 22] Invalid argument
#1
When trying to write to a log file, i get the error OSError: [Errno 22] Invalid argument

The code i am using is as follows:
import datetime
import logging
logger = logging.getLogger('k')
hdlr = logging.FileHandler('Path to the log file/Logs/log'+str(datetime.datetime.now())+'.log')
formatter = logging.Formatter('At %(asctime)s, the program returned %(message)s at level %(levelname)s
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.INFO)
logger.info('Hello')
Self-taught HTML, CSS, Python, and Java programmer
Reply
#2
Does the 'Path to the log file' directory exists in your working directory?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
(Jun-03-2018, 02:09 PM)wavic Wrote: Does the 'Path to the log file' directory exists in your working directory?

Yes
Self-taught HTML, CSS, Python, and Java programmer
Reply
#4
You have a directory called 'Path to the log file' and its subdir 'Logs'?

Post the full error traceback in error tags, please.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
Error:
Traceback (most recent call last): File "C:\Users\Richard\Desktop\Atom\K\K.py", line 20, in <module> hdlr = logging.FileHandler('/Users/Richard/Desktop/Atom/K/Logs/log'+str(datetime.datetime.now())+'.log') File "C:\Users\Richard\AppData\Local\Programs\Python\Python36-32\lib\logging\__init__.py", line 1025, in __init__ StreamHandler.__init__(self, self._open()) File "C:\Users\Richard\AppData\Local\Programs\Python\Python36-32\lib\logging\__init__.py", line 1054, in _open return open(self.baseFilename, self.mode, encoding=self.encoding) OSError: [Errno 22] Invalid argument: 'C:\\Users\\Richard\\Desktop\\Atom\\K\\Logs\\log2018-06-03 09:53:07.712997.log'
Self-taught HTML, CSS, Python, and Java programmer
Reply
#6
I presume that you are following some book or tutorial.

On line 4 in your code, instead of 'Path to the log file' put your path 'C:/Users/Richard/Desktop/Atom/K'.
hdlr = logging.FileHandler(''C:/Users/Richard/Desktop/Atom/K/Logs/log'+str(datetime.datetime.now())+'.log')
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
(Jun-03-2018, 02:21 PM)wavic Wrote: I presume that you are following some book or tutorial.

On line 4 in your code, instead of 'Path to the log file' put your path 'C:/Users/Richard/Desktop/Atom/K'.
hdlr = logging.FileHandler(''C:/Users/Richard/Desktop/Atom/K/Logs/log'+str(datetime.datetime.now())+'.log')

That is what is there. I still get the error.
Self-taught HTML, CSS, Python, and Java programmer
Reply
#8
Yea I left one quote when I pasted it. The path to the file is not 'Path to the log file/Logs' but 'C:/Users/Richard/Desktop/Atom/K/Logs'

hdlr = logging.FileHandler('C:/Users/Richard/Desktop/Atom/K/Logs/log'+str(datetime.datetime.now())+'.log')
If you give you an error post it.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#9
(Jun-03-2018, 02:30 PM)wavic Wrote: Yea I left one quote when I pasted it. The path to the file is not 'Path to the log file/Logs' but 'C:/Users/Richard/Desktop/Atom/K/Logs'

hdlr = logging.FileHandler('C:/Users/Richard/Desktop/Atom/K/Logs/log'+str(datetime.datetime.now())+'.log')
If you give you an error post it.

Perhaps you don't understand. This was there the entire time. I just didn't say the full path.
Self-taught HTML, CSS, Python, and Java programmer
Reply
#10
the colon is not allowed in file name. Use something like
hdlr = logging.FileHandler('Path to the log file/Logs/log{}.log'.format(datetime.datetime.strftime(datetime.datetime.now(), '%Y%m%d%H%M%S_%f'))
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  OSError occurs in Linux. anna17 2 306 Mar-23-2024, 10:00 PM
Last Post: snippsat
  Invalid argument: 'images\x08ackground.jpg' CatBall 4 968 Jun-19-2023, 09:28 AM
Last Post: CatBall
  OSError with SMPT script Milan 0 779 Apr-28-2023, 01:34 PM
Last Post: Milan
  OSERROR When mkdir Oshadha 4 1,711 Jun-29-2022, 08:50 AM
Last Post: DeaD_EyE
  [split] [Errno 22] Invalid argument Junaid 0 2,290 Jun-12-2021, 06:02 PM
Last Post: Junaid
  [solved] Variable number of dictionnaries as argument in def() paul18fr 11 6,155 Apr-20-2021, 11:15 AM
Last Post: paul18fr
  OSError: Unable to load libjvm when connecting to hdfs with pyarrow 3.0.0 aupres 0 3,171 Mar-22-2021, 10:25 AM
Last Post: aupres
  pyarrow throws oserror winerror 193 1 is not a valid win32 application aupres 2 3,787 Oct-21-2020, 01:04 AM
Last Post: aupres
  [Errno 22] Invalid argument satyaneel 11 106,253 Jul-14-2020, 08:47 AM
Last Post: lichunming
  zlib decompress error: invalid code lengths set / invalid block type DreamingInsanity 0 6,868 Mar-29-2020, 12:44 PM
Last Post: DreamingInsanity

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020