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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:39:06+00:00 2026-06-08T16:39:06+00:00

I have a script that renames files before uploading them to an FTP. First

  • 0

I have a script that renames files before uploading them to an FTP. First it searched for the pattern “_768x432_1700_m30_” and if it find it the pattern gets replaced by “new” – then it uploads all “.mp4” files in the directory to an FTP server. But for some reason I can’t seem to delete the files after them have been uploaded? Also is there a better way of doing this script? (I am fairly new to python)

#!/usr/bin/python

import os
import glob
import fnmatch
import sys
import ftplib
import shutil
import re
from ftplib import FTP



Host='xxxxxx.xxxxx.xxxx.com'
User='xxxxxxx'
Passwd='xxxxxxx'

ftp = ftplib.FTP(Host,User,Passwd) # Connect


dest_dir = '/8619/_!/xxxx/xx/xxxxx/xxxxxx/xxxx/'
Origin_dir = '/8619/_!/xxxx/xx/xxxxx/xxxxxx/xxxx/'
pattern = '*.mp4'
file_list = os.listdir(Origin_dir)


for filename in glob.glob(os.path.join(Origin_dir, "*_768x432_1700_m30_*")):
    os.rename(filename, filename.replace('_768x432_1700_m30_','_new_' ))
    video_list = fnmatch.filter(filename, pattern)

print(video_list)

print "Checking %s for files" % Origin_dir
for files in file_list:
    if fnmatch.fnmatch(files, pattern):
        print(files)
        print "logging into %s FTP" % Host
        ftp = FTP(Host)
        ftp.login(User, Passwd)
        ftp.cwd(dest_dir)
        print "uploading files to %s" % Host
        ftp.storbinary('STOR ' + dest_dir+files, open(Origin_dir+files, "rb"), 1024)
        ftp.close
        print 'FTP connection has been closed'
  • 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-08T16:39:07+00:00Added an answer on June 8, 2026 at 4:39 pm

    On the following line
    ftp.storbinary('STOR ' + dest_dir+files, open(Origin_dir+files, "rb"), 1024)
    you open a file, but you don’t keep a reference to it and close it. On Windows (I assume you are running this on Windows), a file can not be deleted while a process has it open.

    Try the following instead:

    print "uploading files to %s" % Host
    with open(Origin_dir+files, "rb") as f:
        ftp.storbinary('STOR ' + dest_dir+files, f, 1024)
    ftp.close()
    print 'FTP connection has been closed'
    

    The differences are:

    • use a with-statement to ensure the file is closed whether successful or an exception is raised
    • assign the result of the open() call to a name (f)
    • added missing parenthesis to ftp.close() so the function is called.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following script that executes all the .reg files in the current directory.
1.Hi, I have a internal use only file upload script that uploads the files
I have a script which moves and renames files from an AS400 directory to
I have a script partially based on the one here: Upload files with FTP
I have an upload PHP script and before it uploads the file, it renames
I'm making a CLI script that mass renames files, however I do not know
I have a PHP script that moves files out of a specific folder on
Most of the vendors that FTP files to us we have the vendor place
We have a program that take specific XML files and imports them, then changes
I have this script that I'm running to rename all the files in a

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.