Renaming of files in OS directory
Hi, all, I am writing a program that renames files inside OS directories the user provides. I am at the early stage of writing it and I encountered some problems. Below is my code. There is an error i received when i run this code. The error is, WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect. Hope you guys could help. import os, glob def fileDirectory(): #Asks the user for a file root directory fileroot = raw_input("Please input the file root directory \n\n") print fileroot #Returns a list with all the files inside the file root directory os.listdir(fileroot) fileDirectory() -- http://mail.python.org/mailman/listinfo/python-list
Re: Renaming of files in OS directory
On Aug 8, 4:15 pm, Chris Rebert wrote: > On Sun, Aug 8, 2010 at 1:02 AM, blur959 wrote: > > Hi, all, I am writing a program that renames files inside OS > > directories the user provides. I am at the early stage of writing it > > and I encountered some problems. > > > Below is my code. There is an error i received when i run this code. > > The error is, WindowsError: [Error 123] The filename, directory name, > > or volume label syntax is incorrect. > > Well, what directory did you input? Apparently it wasn't a valid or extant > one. > > Cheers, > Chris > --http://blog.rebertia.com I input for e.g, "C:" it works, basically, if i input a hard code string inside os.listdir it works, but if i stored the string that the user keyed inside a variable and run os.listdir with the variable, there is that error. But inputing hard code string inside os.listdir isn't what I want when I am writing this program. -- http://mail.python.org/mailman/listinfo/python-list
Re: Renaming of files in OS directory
On Aug 8, 6:05 pm, Thomas Jollans wrote: > On 08/08/2010 10:35 AM, blur959 wrote: > > > > > On Aug 8, 4:15 pm, Chris Rebert wrote: > >> On Sun, Aug 8, 2010 at 1:02 AM, blur959 wrote: > >>> Hi, all, I am writing a program that renames files inside OS > >>> directories the user provides. I am at the early stage of writing it > >>> and I encountered some problems. > > >>> Below is my code. There is an error i received when i run this code. > >>> The error is, WindowsError: [Error 123] The filename, directory name, > >>> or volume label syntax is incorrect. > > >> Well, what directory did you input? Apparently it wasn't a valid or extant > >> one. > > >> Cheers, > >> Chris > >> --http://blog.rebertia.com > > > I input for e.g, "C:" it works, basically, if i input a hard code > > string inside os.listdir it works, but if i stored the string that the > > user keyed inside a variable and run os.listdir with the variable, > > there is that error. But inputing hard code string inside os.listdir > > isn't what I want when I am writing this program. > > You didn't answert the question. What is the actual string you pass to > os.listdir after you got it from the user? You could > print repr(fileroot) > to find out. > > My tentative guess is that maybe Windows doesn't like newlines in file > names (I know UNIX allows them, but they're still usually a bad idea) > and maybe you string ends with a newline. I do not get what you mean. The string i passed in is stored inside the variable fileroot. In the case I tested, i inputed the string "C: \" inside the raw_input and stored it inside fileroot, I tried printing repr(fileroot) and it gave me "C:\" as the result and when i tried running os.listdir(fileroot) i got the error. The string i passed to os.listdir is the string i keyed inside fileroot under the raw_input? -- http://mail.python.org/mailman/listinfo/python-list
Re: Renaming of files in OS directory
On Aug 8, 7:45 pm, Thomas Jollans wrote: > On 08/08/2010 12:23 PM, blur959 wrote: > > > > > On Aug 8, 6:05 pm, Thomas Jollans wrote: > >> On 08/08/2010 10:35 AM, blur959 wrote: > > >>> On Aug 8, 4:15 pm, Chris Rebert wrote: > >>>> On Sun, Aug 8, 2010 at 1:02 AM, blur959 wrote: > >>>>> Hi, all, I am writing a program that renames files inside OS > >>>>> directories the user provides. I am at the early stage of writing it > >>>>> and I encountered some problems. > > >>>>> Below is my code. There is an error i received when i run this code. > >>>>> The error is, WindowsError: [Error 123] The filename, directory name, > >>>>> or volume label syntax is incorrect. > > >>>> Well, what directory did you input? Apparently it wasn't a valid or > >>>> extant one. > > >>>> Cheers, > >>>> Chris > >>>> --http://blog.rebertia.com > > >>> I input for e.g, "C:" it works, basically, if i input a hard code > >>> string inside os.listdir it works, but if i stored the string that the > >>> user keyed inside a variable and run os.listdir with the variable, > >>> there is that error. But inputing hard code string inside os.listdir > >>> isn't what I want when I am writing this program. > > >> You didn't answert the question. What is the actual string you pass to > >> os.listdir after you got it from the user? You could > >> print repr(fileroot) > >> to find out. > > >> My tentative guess is that maybe Windows doesn't like newlines in file > >> names (I know UNIX allows them, but they're still usually a bad idea) > >> and maybe you string ends with a newline. > > > I do not get what you mean. The string i passed in is stored inside > > the variable fileroot. In the case I tested, i inputed the string "C: > > \" inside the raw_input and stored it inside fileroot, I tried > > printing repr(fileroot) and it gave me "C:\" as the result and when i > > tried running os.listdir(fileroot) i got the error. The string i > > passed to os.listdir is the string i keyed inside fileroot under the > > raw_input? > > You are passing a string to os.listdir. (you call that string fileroot). > There is probably something wrong with that string. In principle, it > doesn't matter where you got the string from - with raw_input() or by > hard-coding the string. > > repr(fileroot) is almost certainly not "C:\" -- that is not a valid > string literal. > > What did you enter exactly? > > -- Thomas Sorry, This is my first time using the os commands in python, Ok, firstly, I entered "C:\" inside raw_input and stored it inside fileroot. When i print repr(fileroot), my result was '"C:\\"' . And when I run os.listdir with fileroot, I got that error. I typed my os.listdir code like this: os.listdir(fileroot) I attached my code for reference, thanks again! import os, glob def fileDirectory(): # Ask user for file directory input fileroot = raw_input("Input") print repr(fileroot) #Returns a list with all the files inside the file root directory( The error occurs here ) os.listdir(fileroot) fileDirectory() -- http://mail.python.org/mailman/listinfo/python-list
Re: Renaming of files in OS directory
On Aug 8, 9:13 pm, Thomas Jollans wrote: > On 08/08/2010 02:35 PM, blur959 wrote: > > > Sorry, This is my first time using the os commands in python, Ok, > > firstly, I entered "C:\" inside raw_input and stored it inside > > fileroot. When i print repr(fileroot), my result was '"C:\\"' . And > > when I run os.listdir with fileroot, I got that error. I typed my > > os.listdir code like this: os.listdir(fileroot) > > > I attached my code for reference, thanks again! > > Okay, maybe you've already understood the problem now, but in case you > haven't: > '"C:\\"' is no valid file name. The quotes ("") are part of what you're > passing to the OS here, which you don't want. Just enter the file name > without quotes. (or, if you really want quoting for some reason, you > could manually strip the quotes, or use the shlex module. > > > > > import os, glob > > > def fileDirectory(): > > # Ask user for file directory input > > fileroot = raw_input("Input") > > print repr(fileroot) > > > #Returns a list with all the files inside the file root > > directory( The error occurs here ) > > os.listdir(fileroot) > > > fileDirectory() > > okay i got it already thanks alot man! Appreciate it! -- http://mail.python.org/mailman/listinfo/python-list
simple renaming files program
Hi, all, I am working on a simple program that renames files based on the directory the user gives, the names the user searched and the names the user want to replace. However, I encounter some problems. When I try running the script, when it gets to the os.rename part, there will be an error. The error is : n = os.rename(file, file.replace(s, r)) WindowsError: [Error 2] The system cannot find the file specified I attached my code below, hope you guys can help me, Thanks! import os directory = raw_input("input file directory") s = raw_input("search for name") r = raw_input("replace name") for file in os.listdir(directory): n = os.rename(file, file.replace(s, r)) print n -- http://mail.python.org/mailman/listinfo/python-list
Re: simple renaming files program
On Aug 9, 6:01 pm, Chris Rebert wrote: > On Mon, Aug 9, 2010 at 2:44 AM, blur959 wrote: > > Hi, all, I am working on a simple program that renames files based on > > the directory the user gives, the names the user searched and the > > names the user want to replace. However, I encounter some problems. > > When I try running the script, when it gets to the os.rename part, > > there will be an error. The error is : > > n = os.rename(file, file.replace(s, r)) > > WindowsError: [Error 2] The system cannot find the file specified > > > I attached my code below, hope you guys can help me, Thanks! > > > import os > > directory = raw_input("input file directory") > > s = raw_input("search for name") > > r = raw_input("replace name") > > > for file in os.listdir(directory): > > n = os.rename(file, file.replace(s, r)) > > print n > > os.rename() takes paths that are absolute (or possibly relative to the > cwd), not paths that are relative to some arbitrary directory (as > returned by os.listdir()). > Also, never name a variable "file"; it shadows the name of the built-in type. > > Hence (untested): > from os import listdir, rename > from os.path import isdir, join > directory = raw_input("input file directory") > s = raw_input("search for name") > r = raw_input("replace name") > > for filename in listdir(directory): > path = join(directory, filename) #paste the directory name on > if isdir(path): continue #skip subdirectories (they're not files) > newname = filename.replace(s, r) > newpath = join(directory, newname) > n = rename(path, newpath) > print n > > Cheers, > Chris > --http://blog.rebertia.com Thanks, they worked! -- http://mail.python.org/mailman/listinfo/python-list
Creating a custom UI inside Maya with python
Hi, all, I wonder if my post is relevant here, but i will still post it anyway. I am working on creating a custom UI inside Maya and I encountered some problems. Firstly, I am trying to create a textfield button that creates a locator-shaped curve based on the coordinates the user keyed into the text field. However I got no idea how to go about doing it properly. I hope you guys could give me some help. Thanks. I attached my code below. My code isn't working though. I have this error, which says button2 is not defined. I got no clue on how else to debug. import maya.cmds as cmds def createMyLayout(): window = cmds.window(widthHeight=(1000, 600), title="test", resizeToFitChildren=1) cmds.rowLayout("button1, button2, button3", numberOfColumns=5) cmds.columnLayout(adjustableColumn=True, columnAlign="center", rowSpacing=10) button2 = cmds.textFieldButtonGrp(label="LocatorCurve", text="Please key in your coordinates", changeCommand=edit_curve, buttonLabel="Execute", buttonCommand=locator_curve) cmds.setParent(menu=True) cmds.showWindow(window) def locator_curve(*args): # Coordinates of the locator-shaped curve. crv = cmds.curve(degree=1, point=[(1, 0, 0), (-1, 0, 0), (0, 0, 0), (0, 1, 0), (0, -1, 0), (0, 0, 0), (0, 0, 1), (0, 0, -1), (0, 0, 0)]) return crv def edit_curve(*args): parts = button2.split(",") print parts x = parts[0] y = parts[1] z = parts[2] createMyLayout() -- http://mail.python.org/mailman/listinfo/python-list
Renaming OS files by file type in python
Hi all, I am creating a program that renames all files of the similar file type. But i am stuck at this part. I tried running this code and I got this error:new_name = os.rename(path, newpath) WindowsError: [Error 183] Cannot create a file when that file already exists. Hope you guys could help. import os directory = raw_input("Please input file directory. \n\n") s = raw_input("Please input a name to replace. \n\n") ext = raw_input("input file ext") for filename in listdir(directory): if ext in filename: path = join(directory, filename) fnpart = os.path.splitext(filename)[0] replace_name = filename.replace(fnpart, s) newpath = os.path.join(directory, replace_name) new_name = os.rename(path, newpath) print new_name -- http://mail.python.org/mailman/listinfo/python-list
How do I get number of files in a particular directory.
Hi, all, Is there a way to get a number of files in a particular directory? I tried using os.walk, os.listdir but they are return me with a list, tuple of the files, etc. But I want it to return a number. Is it possible? -- http://mail.python.org/mailman/listinfo/python-list
Re: How do I get number of files in a particular directory.
Hi, I tried that, but it doesn't seem to work. My file directory has many different files extensions, and I want it to return me a number based on the number of files with similar files extensions. But when I tried running it, I get many weird numbers. Below is my code i had so far and the result. import os directory = raw_input("Please input file directory. \n\n") s = raw_input("Please input a name to replace. \n\n") ext = raw_input("input file ext") for files in os.listdir(directory): if ext in files: file_number = len(files) print file_number The result is: 13 13 13 6 15 8 10 10 8 7 5 where the result should be just 11. Can anyone help me? Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: How do I get number of files in a particular directory.
On Aug 13, 6:09 pm, Peter Otten <__pete...@web.de> wrote: > blur959 wrote: > > Hi, I tried that, but it doesn't seem to work. My file directory has > > many different files extensions, and I want it to return me a number > > based on the number of files with similar files extensions. But when I > > tried running it, I get many weird numbers. Below is my code i had so > > far and the result. > > Use glob.glob() instead of os.listdir() if you are only interested in files > with a specific extension: > > >>> import glob > >>> len(glob.glob("*.py")) > > 42 > > Peter Hi, I want to make it such that the user inputs a file extension and it prints the number of similar file extensions out. I tried doing this: directory = raw_input("input file directory") ext = raw_input("input file ext") file_list = len(glob.glob(ext)) print file_list And my result was this: 0 which it is suppose to be 11 May I know why? And how do I specify which directory it is searching the files extension from? I want the user to specify a directory and it searches the particular directory for the particular file extensions and prints the number out. Hope you guys could help. Thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: How do I get number of files in a particular directory.
Hi all, I got a problem with my script. Everything looks good so far but for some reason my os.rename isn't working. Can anyone tell me why? Hope you guys could help. Thanks. import os import glob directory = raw_input("directory? ") ext = raw_input("file extension? ") r = raw_input("replace name") pattern = os.path.join(directory, "*" + ext) matching_files = glob.glob(pattern) file_number = len(matching_files) for filename in os.listdir(directory): if ext in filename: path = os.path.join(directory, filename) seperated_names = os.path.splitext(filename)[0] replace_name = filename.replace(seperated_names, r) split_new_names = os.path.splitext(replace_name)[0] for pad_number in range(0, file_number): padded_numbers = "%04d" % pad_number padded_names = "%s_%s" % (split_new_names, padded_numbers) newpath = os.path.join(directory, padded_names) newpathext = "%s%s" % (newpath, ext) new_name = os.rename(path, newpathext) -- http://mail.python.org/mailman/listinfo/python-list