I'm fairly new to python scripting and I want to verify file names in a directory and subdirectory.
The verification should be case sensitive.
I'm using python 2.6.5
OS: win7 and xp
I prompt for the following user input:
prompt = "year"
year = raw_input(prompt)
prompt = "number"
number = raw_input(prompt)
From here I want to search/verify that the following files and folders exist and their filename is correct.
folderstructure:
..\foobar_(number)_version1\music
Files in subfolder 'music'
(year)_foobar_(number)_isnice.txt
(year)_itis(number)hot_today.txt
(year)_anything_is(number)possible.txt
(year)_something_{idont_want_to_check_this_part}_(number)_canbe_anything.txt
Note that all text including underscores are always the same, and thus should always be right, except for the things between () or {}.
I want to output the results to a txt file which reports if the filename is correct or not.
What is the most logical method to archieve this?
I've read the lib documentation fnmatch(.fnmatchcase), RE and os(.path.isfile) and searched here for examples, but I just can't figure out where and how to start.
Can anyone point me in the right direction?
[edit]
As soon as my script has the working basics I'll post my code for reference or to help others.
[edit2] my first non-hello world script
import os
import re
#output :
file_out = "H:\\output.txt"
f_out = open(file_out, 'w')
print "-------start-script----------"
#input
prompt = "enter 4 digit year: "
year = raw_input(prompt)
prompt = "enter 2 digit number: "
number = raw_input(prompt)
print "the chosen year is %s" % (year)
print "the chosen number is %s" % (number)
f_out.write ("start log!\n")
f_out.write ("------------------------------------------\n")
f_out.write ("the chosen year is %s\n" % (year))
f_out.write ("the chosen number is %s\n" % (number))
#part i'm working on
print "end script"
f_out.write ("------------------------------------------\n")
f_out.write ("end script\n")
#close file
f_out.close()
Take a look at the glob module - this will help you get a list of files in the current directory:
import glob
year = raw_input('Year: ') # Example: Year: 2009
number = raw_input('Number: ') # Example: Number: 12
filenames = glob.glob('{year}_*{number}*'.format(year=year, number=number))
Filenames will be anything in the current directory that meets the following criteria:
Begins with 2009_
Any number of characters until it matches 12
Any number of characters following 12.
os.path.exists is a good way to check if the file exists, or os.path.isfile if you want to make sure that it's really a file and not a directory named like a file. For Python3, check these docs, and like the link ghostbust555 mentioned says, be careful of race conditions if you plan on doing anything besides verifying their existence.
Based on your comment, it looks like this is a job for regular expressions. The pseudo code for what you need to write looks something like this:
for filename in list of filenames:
if filename is not valid:
print "<filename> is not valid!"
Aside from the actual pattern, the actual python code could look like this:
import os
import re
pattern = 'Put your actual pattern here'
# For a different directory, change the . to whatever the directory should be
for filename in os.listdir('.'):
if not re.match(pattern, filename):
print("Bad filename: ", filename)
This is not meant to be a full answer, but an extension of #Wayne Werner's answer. I don't have enough reputation points yet to comment. ;0
Wayne's approach using format I think is pointing to what you should do because it's
validating filename BEFORE the files are built instead of after. And it seems that's what you're doing and have control over?
I would do as much validation at the user input level as possible.
Validate the other parts from whereever you get them.
Build a dictionary with the parts.
Build your file_name.
For example, at the user input level, something like:
yourDict = dict()
year_input = raw_input('What is the year'?)
if not year_input.isdigit():
year_input = raw_input('Only digits please in the format YYYY, example: 2012'):
yourDict[year] = year_input
Then continute to add key:values to yourDict by validating the other values by whatever criteria it is you have. (Use re module or other method's mentioned).
Then, as Wayne was doing, use .format() with a passed in dictionary to map to the correct parts.
format1 = "{year}{part1}{number}{part2}.txt".format(**yourDict)
The approach also allows you to quickly build new formats with the same parts, and you can pick and choose what keys in the dictionary you need or don't need for each format.
Hope that's helpful.
import os.path
year = 2009
file1 = year + "_foobar_" + number + "_isnice.txt"
os.path.exists(file1)
Related
Why is this basic rename script not doing what it should?
Just trying to capitalize first letter of each word.
import glob
import os
for filename in glob.glob("**/*.mp3", recursive = True):
withcap = str(filename).title()
print("nc " +(filename))
print("wc " +(withcap))
os.rename(filename, withcap)
The output from the print is correct but nothing happens at os.rename?
output:
nc BLOOD COMMAND - Return Of The Arsonist [Clean].mp3
wc Blood Command - Return Of The Arsonist [Clean].Mp3
This can happen if you're on an operating system with a case-insensitive filesystem -- like Windows -- where both original and destination names already show us as both existing and pointing to the same file.
A workaround is simply to rename through a temporary name that differs in more than case:
for filename in glob.glob("**/*.mp3", recursive = True):
withcap = str(filename).title()
os.rename(filename, withcap+'.tmp')
os.rename(withcap+'.tmp', withcap)
As mentioned in the comments, the cause is that you are working with a file system that is case insensitive. It considers the old and new name to be the same, so the "rename" becomes a no-op.
You'll have to do two renames for each file: first to a different, temporary name; then to the actual name with modified capitalization.
I made a Python program that reads all the files in a directory who's path is inputted and then does some things to them and outputs some things into a .txt file.
I used os.walk to read the files in the directory
def dir_read(pathName = str(), fileList=[]): #fileList is output
cur_dir = [i[2] for i in os.walk(pathName)] #current directory
for i in cur_dir:
for j in i:
fileList.append(j)
and then checking for correctly inputted paths like this
fileList=[]
while True:
dir_read(pathName, fileList) #FUNCTION CALL
if not fileList: #if it is empty
print("The path is written incorrectly, is empty or does not exist. Please re-enter now or "
"close the program and enter it inside PATH.txt:")
pathName = str(input())
else:
break #if the list is not empty (the path is entered correctly) the while loop breaks
However if I enter something like This PC\Nokia 6.1\Card SD it keeps giving me the error message. Also I noticed same thing happens with things like Desktop or Documents. But if I enter something like C:\Program Files (x86)\Notepad++ it works perfectly.
I tried replacing \ with / as well and it doesn't work.
My phone doesn't have a specific drive like F: as you can see here: https://imgur.com/a/Educ5RJ
I am using Windows 10.
How do I fix this?
I believe your directory should be like this:
on Linux:
media/This PC/Nokia 6.1/Card SD
on Windows:
You need to know the drive letter you can run this command:
C:\>wmic logicaldisk where drivetype=2 get deviceid, volumename, description
let's say it returned X:
then your directory should be something like this:
X:\Nokia 6.1\Card SD
I'm not sure if you are supposed to remove the Nokia 6.1 or not
try Using subst command and if you have spaces on your device's name put it on quotes
Ex:
subst E: "The Name Goes Here"
I'm a noob at coding Python and I've run into something that no amount of Googling is helping me with.
I'm trying to write a simple Directory listing tool and I cannot seem to deal with Spaces in the directory name in OSX.
My code is as follows:
def listdir_nohidden(path):
import os
for f in os.listdir(path):
if not f.startswith('.'):
yield f
def MACListDirNoExt():
import os
MACu = PCu = os.environ['USER']
MACDIR = '/Users/'+MACu+'/Desktop//'
while True:
PATH = raw_input("What is the PATH you would like to list?")
if os.path.exists(PATH):
break
else:
print "That PATH cannot be found or does not exist."
NAME = raw_input ("What would you like to name your file?")
DIR = listdir_nohidden(PATH)
DIR = [os.path.splitext(x)[0] for x in DIR]
f = open(''+MACDIR+NAME+'.txt', "w")
for file in DIR:
f.write(str(file) + "\n")
f.close()
print "The file %s.txt has been written to your Desktop" % (NAME)
raw_input ("Press Enter to exit")
For ease of trouble shooting though I think this could essentially be boiled down to:
import os
PATH = raw_input("What is the PATH you would like to list")
os.listdir(PATH)
When supplying a directory path that contains spaces /Volumes/Disk/this is a folder it returns
"No such file or Directory: '/Volumes/Disk/this\\ is\\ a\\ folder/'
It looks like its escaping the escape...?
Check the value returned from raw_input() for occurences of '\\' and replace them with ''.
a = a.replace('\\', '')
I just ran into this, and I'm guessing that what I was hastily doing is also what you were trying. In a way, both #zwol and #trans1st0r are right.
Your boiled down program has nothing wrong with it. I believe that if you put in the input /Volumes/Disk/this is a folder, everything would work fine.
However, what you may have been doing (or at least, what I was doing) is dragging a folder from the Finder to the Terminal. When you drag to the Terminal, the OS automatically escapes spaces for you, so what ends up getting typed into the Terminal is /Volumes/Disk/this\ is\ a\ folder.
So either you can make sure that what you "type in" doesn't have those backslashes, or you can use #trans1st0r's suggestion as a way to support the dragging functionality, though the latter will cause issues in the edge case that your desired path actually has backslashes in it.
Hey guys Im trying to make a simple program which cleans up directorys gathered by user input. AKA User inputs "E:" for deleting and "F:" for deleting. But I cant get it to work Using shutil.
I cant seem to find a way to store the user inputs in a way which lets shutil use it for deleting!
Im really new to python and programming in general so im probably doing some stupid mistake.
EDIT: I need it so The user inputs gets stored in a form of list, then I can run shutil to delete all the directoris stored in the list
import shutil
DeleteThis = {}
def DirectoryToDelete():
DirectoryToEnter = input('Enter Filepath of directory you want to delete: ')
DeleteThis.append(DirectoryToEnter)
print (DeleteThis)
shutil.rmtree(DeleteThis)
Use string for shutil and check your directory with os.path.isdir:
import shutil
import os.path
while True:
directory = input('Enter Filepath of directory you want to delete: ')
if os.path.isdir(directory):
shutil.rmtree(directory)
print "Directory was deleted!"
else:
print "Bad directory"
A modification of JRazors answer:
import shutil
import os.path
import readline
#allow tab completion in input
readline.parse_and_bind("tab: complete")
delete_this = input('Enter Directories you want to delete separated by comma: ')
for d in delete_this.split(','):
d=d.strip()
if os.path.isdir(d):
shutil.rmtree(d)
print("{} deleted".format(d))
This looks through the text separated by comma to find directories you want to delete.
Use example
python delete_dir.py
Enter Directories you want to delete separated by comma: fo<tab>
foo1/ foo2/ foo3.txt
Enter Directories you want to delete separated by comma: foo1,foo2
foo1 deleted
foo2 deleted
I'm writing a script to save a file and giving the option of where to save this file. Now if the directory entered does NOT exist, it breaks the code and prints the Traceback that the directory does not exist. Instead of breaking the code I'd love to have have a solution to tell the user the directory they chose does not exist and ask again where to save the file. I do not want to have this script make a new directory. Here is my code:
myDir = input('Where do you want to write the file?')
myPath = os.path.join(myDir, 'foobar.txt')
try:
f = open(myPath, 'w')
except not os.path.exists(myDir):
print ('That directory or file does not exist, please try again.')
myPath = os.path.join(myDir, 'foobar.txt')
f.seek(0)
f.write(data)
f.close()
This will get you started however I will note that I think you need to tweak the input statement so the user does not have to put quotes in to not get an invalid syntax error
import os
myDir = input("Where do you want to write the file ")
while not os.path.exists(myDir):
print 'invalid path'
myDir = input("Where do you want to write the file ")
else:
print 'hello'
I do not know what your programming background is - mine was SAS before I found Python and some of the constructions are just hard to think through because the approach in Python is so much simpler without having a goto or similar statement but I am still trying to add a goto approach and then someone reminds me about how the while and while not are simpler, easier to read etc.
Good luck
I should add that you really don't need the else statement I put it there to test/verify that my code would work. If you expect to do something like this often then you should put it in a function
def verify_path(some_path_string):
while not os.path.exists(some_path_string):
print 'some warning message'
some_path_string = input('Prompt to get path')
return some_path_string
if _name_ == main:
some_path_string = input("Prompt to get path")
some_path_string = verify_path(some_path_string)