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 6958313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:07:24+00:00 2026-05-27T15:07:24+00:00

I have this function that references the path of a file: some_obj.file_name(FILE_PATH) where FILE_PATH

  • 0

I have this function that references the path of a file:

some_obj.file_name(FILE_PATH)

where FILE_PATH is a string of the path of a file, i.e. H:/path/FILE_NAME.ext

I want to create a file FILE_NAME.ext inside my python script with the content of a string:

some_string = 'this is some content'

How to go about this? The Python script will be placed inside a Linux box.

  • 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-05-27T15:07:25+00:00Added an answer on May 27, 2026 at 3:07 pm

    There is a tempfile module for python, but a simple file creation also does the trick:

    new_file = open("path/to/FILE_NAME.ext", "w")
    

    Now you can write to it using the write method:

    new_file.write('this is some content')
    

    With the tempfile module this might look like this:

    import tempfile
    
    new_file, filename = tempfile.mkstemp()
    
    print(filename)
    
    os.write(new_file, "this is some content")
    os.close(new_file)
    

    With mkstemp you are responsible for deleting the file after you are done with it. With other arguments, you can influence the directory and name of the file.


    UPDATE

    As rightfully pointed out by Emmet Speer, there are security considerations when using mkstemp, as the client code is responsible for closing/cleaning up the created file. A better way to handle it is the following snippet (as taken from the link):

    import os
    import tempfile
    
    fd, path = tempfile.mkstemp()
    try:
        with os.fdopen(fd, 'w') as tmp:
            # do stuff with temp file
            tmp.write('stuff')
    finally:
        os.remove(path)
    

    The os.fdopen wraps the file descriptor in a Python file object, that closes automatically when the with exits. The call to os.remove deletes the file when no longer needed.

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

Sidebar

Related Questions

I have this function called ProperCase that takes a string, then converts the first
I have this function I'm using and I want to be sure that it
I have this code snippet inside a function that checks if an object exists
I have this function that prints the name of all the files in a
I have this function that converts all special chars to uppercase: function uc_latin1($str) {
So I have this function that forks N number of child processes. However it
I have this function in my Javascript Code that updates html fields with their
I have this function on my controller (Im using CodeIgniter) that reads the database,
I have this function to edit all fields that come from the form and
I have this function, that adds 31px to my style bottom on ul. It

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.