Eddie Eddie - 1 month ago 11
Python Question

Changing Filenames with python, whilst using some of the old name

Im currently working on a Python script wich will upload a file from a directory onto an FTP server wich is build into a 3D Printer. Everything with upload and FTP works, but i have a rather specific task to do for the script. I want to dump a lot of files into the starting directory. So it goes like:


  1. 1474lapfab.tgz

  2. 1475lapfab.tgz

  3. ...lapfab.tgz

  4. ..........



I want the script to grab the first file and modify the name so it only sends lapfab.tgz (lapfab.tgz being the filename the printer reacts to so it has to be this name) to the ftp server. But here is the specific part. I want a new Variable wich holds the number at the beginning, so i can display the current number of project the printer ist working on.

My Code so Far

# -*- coding: cp1252 -*-
#geschrieben in Python 2.7.1
#Drucker Rechts
import ftplib
import os

meinftp = ftplib.FTP("192.168.178.178")
meinftp.login("anonymous")

directory = '/data/todo/' #Ftp Main Directory
meinftp.cwd(directory) #Login to right ftp directory
directory_local='/home/pi/testprogramme/python/' #Local upload directory
filename = 'lapfab.tgz'

print "Open file"
print
file = open(directory_local+filename, 'rb') #Open file in Binary

print "Upload File"

meinftp.storbinary('Stor '+filename, file) #File Transfer

print
print "Upload Complete"
print
print 'The local File ' + filename +' will close.'

file.close()

print
print meinftp.quit() #Kill Conection
print
print 'FTP-Connection cut.'

os.remove('lapfab') #Deleting the main File from server

print
print "Deleting Main File from Server"


If someone could send me on the right track from here on to geht my task done, that would be more than great!

Greetings from Germany and appologys for bad english.

Best Regards Eddie =)

EDIT:

I got it Kind of going with your Code. The code im using now:

#!/usr/bin/python
# -*- coding: cp1252 -*-
#geschrieben in Python 2.7.1
#Drucker Rechts
import ftplib, os, re, fnmatch

meinftp = ftplib.FTP("192.168.178.178")
meinftp.login("anonymous")

directory = '/data/todo/' #Ftp Main Directory
meinftp.cwd(directory) #Login to right ftp directory
directory_local='/home/pi/testprogramme/python/' #Local upload directory
filename = 'lapfab.tgz'

# Capture the matching files.
file_pattern = "*%s" % filename
matches = sorted([local_filename for local_filename in os.listdir(".") \
if fnmatch.fnmatch(local_filename, file_pattern)])

if len(matches) < 0:
# Don ^ ^ t process anything, no files
exit()

# Extract the variable
regex = r"([0-9]+)%s" % filename
match = re.search(regex, matches[0])

# Do something with the file.
print("Uploading job %s." % match.group(1))
full_path = os.path.join(directory_local,matches[0])

# Upload the file etc here.

print "Open file"
print
file = open(full_path, 'rb') #Open file in Binary

print "Upload File"

meinftp.storbinary('Stor '+filename, file) #File Transfer

print
print "Upload Complete"
print
print 'The local File ' + filename +' will close.'

file.close()

print
print meinftp.quit() #Kill Conection
print
print 'FTP-Connection cut.'

os.remove(full_path) #Deleting the main File from server

print
print "Deleting Main File from Server"
print
print "Die Datei" +full_path+ " wurde an den Rechten Drucker gesendet"
print
print
print "Ende!"
print


I was able to upload the file and it will delete itself in the end. It worked all great and dandy and then i uploaded 1500lapfab.tgz and 1501lapfab.tgz

the script would ignore 1498lapfab.tgz and 1499lapfab.tgz in favor of the 1500 and 1501lapfab.tgz. this is not wanted. can i somehow alter the order? i tried to fiddle with your code a bit but only got errors.

Q: How can i upload only the lowest file? (low reffering to the number in the beginning)

but all in all im very happy with your help! thank you so much.
Best Regards,
Eddie =)

Answer

If I've read your question right, there's two parts to solve:

  1. How to upload multiple files from the local directory.
  2. How to extract the variable name from the file name.

For 1, you can use os.listdir() to get a list of files in the local directory. For 2, you can use a regular expression to capture the variable out of the file name. Use a regex capture group in parentheses - () to capture the variable. Regular expressions are in the re module.

import re

regex = r"([0-9]+)%s" % filename # This will give you r"([0-9]+)lapfab.tgz"
match = re.search(regex, "1474lapfab.tgz")
print match.group(0) # This will print out "1474lapfab.tgz".
print match.group(1) # This will print out "1474".

If you combine the two, you can iterate the local directory and capture the variable name:

import os, re, fnmatch

directory_local = '/home/pi/testprogramme/python/' #Local upload directory
filename = 'lapfab.tgz'

# Capture the matching files.
file_pattern = "*%s" % filename
matches = sorted([local_filename for local_filename in os.listdir(".") \
if fnmatch.fnmatch(local_filename, file_pattern)])

if len(matches) < 0:
    # Don’t process anything, no files
    return # or exit()

# Extract the variable
regex = r"([0-9]+)%s" % filename
match = re.search(regex, matches[0])

# Do something with the file.
print("Uploading job %s." % match.group(1))
full_path = os.path.join(directory_local,matches[0])

# Upload the file etc here.

Some more resources on regular expressions you might find helpful:

  • This page has a better overview of using regular expressions in python
  • regex101.com is useful for playing with and debugging regexes to see if they match, outside of your program.