Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news2.arglkargh.de!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.028 X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'great.': 0.07; 'file)': 0.09; 'operator,': 0.09; 'def': 0.12; "'w')": 0.16; 'adjusting': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'script,': 0.16; 'wrote:': 0.18; 'file,': 0.19; 'skip:" 20': 0.27; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; "skip:' 10": 0.31; 'file:': 0.31; 'file': 0.32; 'point.': 0.35; 'received:google.com': 0.35; 'wrong': 0.37; 'skip:o 20': 0.38; 'nov': 0.38; 'to:addr:python- list': 0.38; 'does': 0.39; '12,': 0.39; 'to:addr:python.org': 0.39; 'skip:o 30': 0.61; "you're": 0.61; 'such': 0.63; 'directory:': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=AR1O0LTv07T5tsTGLTwJyduKiLtNsL6IS7rjn1kSQ+Q=; b=nL4nFzyF9H1D9li/VHKN5K69+JnNQAo/jsPBEvnkC+58jEVJGQXmWnG8QrjDy57Aiv yZEXX004NQsdkJO1oFoafn4EuQrDyHLlZ4orkSVggxMKMC2BkqSthbWDLpFpylXfet1e 8AySni38qxQZej+bXFo0Mg35KYQwaNs0Oyq8KddE3/DerkbAT2uTFzhcOpDeN4cMbM1T TGNoJvOSu5hHkyG25NHoGcABMiba34DHntR5ZVd8hZ8jL70LN8g0DRZFU4M8QqqJZ0p+ l4o4fHvS55irDUiIdIhBa+vpczlK4QDVf50hMIJMJshQoF6hKQWSDZZp/McVSG9zUgEB jUeQ== MIME-Version: 1.0 X-Received: by 10.67.22.38 with SMTP id hp6mr33407546pad.53.1384209533947; Mon, 11 Nov 2013 14:38:53 -0800 (PST) In-Reply-To: <521ed3d4-e2f7-4196-947d-61f94573a8ce@googlegroups.com> References: <521ed3d4-e2f7-4196-947d-61f94573a8ce@googlegroups.com> Date: Tue, 12 Nov 2013 09:38:53 +1100 Subject: Re: Creating a function for a directory From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 18 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1384209537 news.xs4all.nl 15997 [2001:888:2000:d::a6]:50072 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:59108 On Tue, Nov 12, 2013 at 9:26 AM, Matt wrote: > So I want to take the file, "desktop/test.txt" and write it to "desktop/newfolder/test.txt". I tried the below script, and it gave me: "IOError: [Errno 2] No such file or directory: 'desktop/%s.txt'". Any suggestions would be great. > > > > def firstdev(file): > in_file = open("desktop/%s.txt") % file > indata = in_file.read() > out_file = open("desktop/newfolder/%s.txt", 'w') % file You're using the % operator, which does your interpolations, at the wrong point. You want to be adjusting the file name, not adjusting the opened file: in_file = open("desktop/%s.txt" % file) out_file = open("desktop/newfolder/%s.txt" % file, 'w') ChrisA