Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8787169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:49:10+00:00 2026-06-13T21:49:10+00:00

This question has to do with the answer to Write file with specific permissions

  • 0

This question has to do with the answer to Write file with specific permissions in Python for opening a file for writing (in python) with specific permissions.

The code in the answer looks like:

with os.fdopen(os.open('foo', os.O_APPEND | os.O_CREAT, 0o644)) as out:
  out.write("hello\n")

This code in 2.7.1 (my company does not have 2.7.3 installed) produces:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
IOError: File not open for writing

os.fdopen has its own mode argument, but setting that doesn’t help:

>>> with os.fdopen(os.open('foo', os.O_APPEND | os.O_CREAT, 0o644), 'a') as out:
...   out.write("hello\n")
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 22] Invalid argument

Long story short, I have not been able to figure out how to actually write to a file that has been opened via os.fdopen and os.open. Any ideas? Known bug in 2.7.1?

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T21:49:11+00:00Added an answer on June 13, 2026 at 9:49 pm

    You must choose one of O_RDONLY, O_WRONLY or O_RDWR as a “basic” mode argument to open().

    You did not explicitly do so, so O_RDONLY (zero on many systems) is assumed. Python’s os.fdopen sees that you have specified a O_RDONLY and O_APPEND, which is a bit silly. Python complains about this combination with the EINVAL (“Invalid argument”) error you see.

    (Indeed, if you strace(1) your script — I’m assuming Linux here — I suspect you’ll see that no “natural” EINVAL is encountered. Instead, python performs your os.open()/open(2), and then checks flags (F_GETFL) on the file descriptor just before raising the exception.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question already has an answer here: view sql that linq-to-sql produces 3 Answers
This question already has an answer here: What are Extension Methods? 5 answers Usage
This question has developed off an answer here . My question therefore is what
An answer to this question has led me to look into using Event Tracing
I'm sure this question has a simple enough answer, but I can't seem to
I'm hoping this question has a very simple answer. I can think of ways
I know this question has been posted before... but I haven't found any answer
Ok, so this question has been asked before here . In the response/answer to
I'm posting this question (and answer) so if anybody else has this problem in
Note: Before reading this question and its answer, please check your input element has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.