I am using your ftputil within a python script to get last modification/creation date of files in directory and I am having few problems and wondered if you could help.
host.stat_cache.resize(200000)
recursive = host.walk(directory, topdown=True, onerror=None)
for root,dirs,files in recursive:
for name in files:
#mctime = host.stat(name).mtime
print name
The above outputs a listing of all files in the directory
host.stat_cache.resize(200000)
recursive = host.walk(directory, topdown=True, onerror=None)
for root,dirs,files in recursive:
for name in files:
if host.path.isfile("name"):
mtime1 = host.stat("name")
mtime2 = host.stat("name").mtime
#if crtime < now -30 * 86400:
#print name + " Was Created " + " " + crtime + " " + mtime
print name + " Was Created " + " " + " " + mtime1 + " " + mtime2
Above produces no output
You've put name in quotes. So Python will always be checking for the literal filename "name", which presumably doesn't exist. You mean:
if host.path.isfile(name):
mtime1 = host.stat(name)
mtime2 = host.stat(name).mtime
Related
newby/hobbycoder here.
I wrote a script which runs through a .M3U files and downloads all referenced media. (previews for my record store).
now i manage to get the correct path from the download function this exact way, but for some weird reason the following loop just returns ".mp3"
#write id3 tags
i = 1
for file in os.listdir("/Users/username/Desktop/transmisson"):
artist = str(nucontainer[i][2].replace("/", ""))
track = str(nucontainer[i][1].replace("/",""))
album = str(nucontainer[i][3].replace("/", ""))
filetype = ".mp3"
fullfilename = "/Users/username/Desktop/transmisson/"
+ artist + " - " + track + " - " + album + filetype
EDIT
artist, track, album, filetype all return the correct string
if i concatenate them, they return what i expect
as soon as i add ".mp3" it al goes pear shaped
EDIT
this gives the same problem.
write id3 tags
i = 0
while i < len(nucontainer):
artist = nucontainer[i][2].replace("/", "")
track = nucontainer[i][1].replace("/","")
album = nucontainer[i][3].replace("/", "")
filename = artist + " - " + track + " - " + album + ".mp3"
print filename
i += 1
Separate the paths in os.path.join with commas.
filename = artist + " - " + track + " - " + album + filetype
fullfilename = os.path.join('/Users/username/Desktop/transmisson/', filename)
Your problem might be that you don't 'return' or store anything. Right now, it just loops through the whole directory. Don't you want to store all the fullfilename data or something?
Edit: You don't seem to actualy do something wih file in the loop. Right now you are only referencing i, but i is not defined in your code.
Edit2: Based on your comment that nucontainer is a list of lists, I would assume that you should do something like this:
for i, file in enumerate(os.listdir("/Users/username/Desktop/transmisson")):
artist = str(nucontainer[i][2].replace("/", ""))
track = str(nucontainer[i][1].replace("/",""))
album = str(nucontainer[i][3].replace("/", ""))
filetype = ".mp3"
fullfilename = "/Users/username/Desktop/transmisson/"
+ artist + " - " + track + " - " + album + filetype
After packing my program I decided to test it out to make sure it worked, a few things happened, but the main issue is with the Save_File.
I use a Save_File.py for data, static save data. However, the frozen python file can't do anything with this file. It can't write to it, or read from it. Writing says saved successful but on load it resets all values to zero again.
Is it normal for any .py file to do this?
Is it an issue in pyinstaller?
Bad freeze process?
Or is there some other reason that the frozen file can't write, read, or interact with files not already inside it? (Save_File was frozen inside and doesn't work, but removing it causes errors, similar to if it never existed).
So the exe can't see outside of itself or change within itself...
Edit: Added the most basic version of the save file, but basically, it gets deleted and rewritten a lot.
def save():
with open("Save_file.py", "a") as file:
file.write("healthy = " + str(healthy) + "\n")
file.write("infected = " + str(infected) + "\n")
file.write("zombies = " + str(zombies) + "\n")
file.write("dead = " + str(dead) + "\n")
file.write("cure = " + str(cure) + "\n")
file.write("week = " + str(week) + "\n")
file.write("infectivity = " + str(infectivity) + "\n")
file.write("infectivity_limit = " + str(infectivity_limit) + "\n")
file.write("severity = " + str(severity) + "\n")
file.write("severity_limit = " + str(severity_limit) + "\n")
file.write("lethality = " + str(lethality) + "\n")
file.write("lethality_limit = " + str(lethality_limit) + "\n")
file.write("weekly_infections = " + str(weekly_infections) + "\n")
file.write("dna_points = " + str(dna_points) + "\n")
file.write("burst = " + str(burst) + "\n")
file.write("burst_price = " + str(burst_price) + "\n")
file.write("necrosis = " + str(necrosis) + "\n")
file.write("necrosis_price = " + str(necrosis_price) + "\n")
file.write("water = " + str(water) + "\n")
file.write("water_price = " + str(water_price) + "\n")
file.write("air = " + str(air) + "\n")
file.write("blood = " + str(blood) + "\n")
file.write("saliva = " + str(saliva) + "\n")
file.write("zombify = " + str(zombify) + "\n")
file.write("rise = " + str(rise) + "\n")
file.write("limit = int(" + str(healthy) + " + " + str(infected) + " + " + str(dead) + " + " + str(zombies) + ")\n")
file.write("old = int(1)\n")
Clear.clear()
WordCore.word_corex("SAVING |", "Save completed successfully")
time.sleep(2)
Clear.clear()
player_menu()
it's probably because the frozen version of the file (somewhere in a .zip file) is loaded and never the one you're writing (works when the files aren't frozen)
That's bad practice to:
- have a zillion global variables to hold your persistent data
- generate code in a python file just to evaluate it back again (it's _self-modifying code_).
If you used C or C++ language, would you generate some code to store your data then compile it in your new executable ? would you declare 300 globals? I don't think so.
You'd be better off with json data format and a dictionary for your variables, that would work for frozen or not frozen:
your dictionary would be like:
variables = {"healthy" : True, "zombies" : 345} # and so on
Access your variables:
if variables["healthy"]: # do something
then save function:
import json
def save():
with open("data.txt", "w") as file:
json.dump(variables,file,indent=3)
creates a text file with data like this:
{
"healthy": true,
"zombies": 345
}
and load function (declaring variables as global to avoid creating the same variable, but local only)
def load():
global variables
with open("data.txt", "r") as file:
variables = json.load(file)
I made a script to rename directories with a name that contains spaces or special characters recursively:
import os
import re
import pdb
def renameInvalid(root):
print("root is: " + root)
for f in os.listdir(root):
if os.path.isdir(f):
old = f
f = f.replace(" ", "_")
f = re.sub(r'[^a-zA-Z0-9-_]', '',f)
if old != f:
print(root + " na substitutie")
os.rename(old,f)
print(root + " na hernoemen")
print("renamed " + old + " to " + f )
#pdb.set_trace()
f = '/' + f
pad = root + f
renameInvalid(str(pad))
mountpunt = os.getcwd()
renameInvalid(mountpunt)
You can test this script by making two directories with names containing spaces. You place one of the directories inside the other and run the script from inside the first directory. The script renames the first directory but generates an OSError on isdir(f).
Does anyone know what is the problem here?
Regards,
I found the answer (thanks to timbaileyjones for his solution).
import os
import re
def renameInvalid(root):
for f in os.listdir(root):
old = f
f = f.replace(" ", "_")
f = re.sub(r'[^a-zA-Z0-9-_]', '',f)
if old != f:
os.rename(old,f)
print("renamed " + old + " to " + f )
if os.path.isdir(f):
os.chdir(f)
renameInvalid(".")
os.chdir("..")
renameInvalid(".")
One should only run this code if they know what they are doing. It renames all the folders and files with whitespace or special characters in the filename.
Regards,
I am working on checking for corrupted PDF in a file system. In the test I am running, there are almost 200k PDF's. It seems like smaller corrupted files alert correctly, but I ran into a large 15 MB file that's corrupted and the code just hangs indefinitely. I've tried setting Strict to False with no luck. It seems like it's the initial opening that's the problem. Rather than doing threads and setting a timeout (which I have tried in the past to little success), I'm hoping there's an alternative.
import PyPDF2, os
from time import gmtime,strftime
path = raw_input("Enter folder path of PDF files:")
t = open(r'c:\pdf_check\log.txt','w')
count = 1
for dirpath,dnames,fnames in os.walk(path):
for file in fnames:
print count
count = count + 1
if file.endswith(".pdf"):
file = os.path.join(dirpath, file)
try:
PyPDF2.PdfFileReader(file,'rb',warndest="c:\test\warning.txt")
except PyPDF2.utils.PdfReadError:
curdate = strftime("%Y-%m-%d %H:%M:%S", gmtime())
t.write(str(curdate) + " " + "-" + " " + file + " " + "-" + " " + "fail" + "\n")
else:
pass
#curdate = strftime("%Y-%m-%d %H:%M:%S", gmtime())
#t.write(str(curdate) + " " + "-" + " " + file + " " + "-" + " " + "pass" + "\n")
t.close()
It looks like there is an issue with PyPDF2. I wasn't able to get it to work, however, I used pdfrw and it did not stop at this point and ran through all couple hundred thousand docs without issue.
I've put together a tkinter form and python script for downloading files from an ftp site. The filenames are in the attribute table of a shapefile, as well as an overall Name that the filenames correspond too. In other words I look up a Name such as "CABOT" and download the filename 34092_18.tif. However, if a Name has an apostrophe, such as "O'KEAN", it's giving me trouble. I try to replace the apostrophe, like I've done in previous scripts, but it doesn't download anything....
whereExp = quadField + " = " + "'" + quadName.replace("'", '"') + "'"
quadFields = ["FILENAME"]
c = arcpy.da.SearchCursor(collarlessQuad, quadFields, whereExp)
for row in c:
tifFile = row[0]
tifName = quadName.replace("'", '') + '_' + tifFile
#fullUrl = ftpUrl + tifFile
local_filename = os.path.join(downloadDir, tifName)
lf = open(local_filename, "wb")
ftp.retrbinary('RETR ' + tifFile, lf.write)
lf.close()
Here is an example of a portion of a script that works fine by replacing the apostrophe....
where_clause = quadField + " = " + "'" + quad.replace("'", '"') + "'"
#out_quad = quad.replace("'", "") + ".shp"
arcpy.MakeFeatureLayer_management(quadTable, "quadLayer")
select_out_feature_class = arcpy.SelectLayerByAttribute_management("quadLayer", "NEW_SELECTION", where_clause)