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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:44:54+00:00 2026-06-06T17:44:54+00:00

So, I am creating a bunch of temporary files from backing up some google

  • 0

So, I am creating a bunch of temporary files from backing up some google docs in python. I have already made the appropriate copies of these files os.copy. Now I am ready to delete them. I created them like this:

    line = entry.id.text
title = entry.title.text
splitLine = line.split('/')
key = splitLine[-1]

backUpDir = R'\\XXX\XXXXX\XXXX\XXXXX\otherFiles\GoogleDocBackUp' + '\\'


today = datetime.date.today()
if not os.path.exists(backUpDir + str(today)):
  os.mkdir(backUpDir + str(today))

backupDir = backUpDir + str(today)

tempfile.tempdir = backupDir
file_path = tempfile.mkstemp(suffix='.xls')    

uri = 'http://docs.google.com/feeds/documents/private/full/%s' % key

spreadsheets_client = gdata.spreadsheet.service.SpreadsheetsService()
spreadsheets_client.email = self.gd_client.email
spreadsheets_client.password = self.gd_client.password
spreadsheets_client.source = "My Fancy Spreadsheet Downloader"
spreadsheets_client.ProgrammaticLogin()


# ...
docEntry = self.gd_client.GetDocumentListEntry(uri)

docs_auth_token = self.gd_client.GetClientLoginToken()
self.gd_client.SetClientLoginToken(spreadsheets_client.GetClientLoginToken())
self.gd_client.Export(docEntry, file_path[1])
shutil.copy(file_path[1], backupDir + '//' + title + '.xls')

self.gd_client.SetClientLoginToken(docs_auth_token)  

os.close(file_path[0])

I had looked at creating a tempfile.Temporary file instead of the mkstemp, but I was getting permissions errors. (I suspect that the temporary file tried to delete the directory that I gave it as well)

So back to the meat of the problem. I try to use os.remove on these temporary files and python will not relinquish its hold on it. (I know that my os.remove code works because I ran that function on some of the left over temporary files and they were deleted without problems.)

I would love some here–If I can delete the temporary files–great. If I can’t, is there anyway to rename a file in place in python?

So far, I’ve gotten some suggestions about grabbing the file handle that the mkstemp SHOULD return…but I am not having any luck. When I look at it in the debugger, I only see an integer and the file path.

UPDATE! I think I fixed it: I just had to call os.close on filepath[0] and it looks like it closed! Thanks everyone! Here is the updated code

Thanks again.

  • 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-06T17:44:56+00:00Added an answer on June 6, 2026 at 5:44 pm

    Sorry if this is something obvious, but just checking, you are closing the files when you are done with them before trying to delete them right?

    mkstemp()

    “returns a tuple containing an OS-level handle to an open file (as
    would be returned by os.open()) and the absolute pathname of that
    file, in that order”

    Python doc source for above

    So I would try the close() call on your file handle before trying to delete it.

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

Sidebar

Related Questions

I have an application creating a bunch of divs through a loop. Each div
I am creating a GridView/DetailsView page. I have a grid that displays a bunch
I'm creating a bunch of migrations, some of which are standard create table or
I am creating a bunch of sprites from label textures dynamically and that is
I've been trying a bunch of stuff with creating zips and files and whatnot
I'm creating a bunch of functions which all effectively do the same thing: long
Creating an a app, where I store bunch of photos in the drawable folder,
I am creating as stored procedure that brings back a bunch of data that
I'm creating a jQuery plugin that needs to do a bunch of stuff the
i'm attempting to instantiate a bunch of sounds by creating a string array containing

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.