omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Just Print File Name

    Pythonista
    2
    5
    1994
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • JasonN
      JasonN last edited by JasonN

      Hi there,

      When I run this simple script, everything works just fine but Pythonista prints the file location instead of just the file name. Does anyone know how to just print the file name? I'm assuming this is an iOS application thing because I don't have the same problem running it through Terminal on the Mac.

      Here's the script:

      from sys import argv
      
      script, file_from, file_to = argv
      
      source = open(file_from)
      source_data = source.read()
      
      print "'%s' is %s bytes." % (script, len(source_data))
      
      destination = open(file_to, 'w')
      destination.write(source_data)
      
      source.close()
      destination.close()
      

      Instead of printing the file name, I get something like:

      /Private/var/mobile/Library/Mobile Documents/iCloud etc

      cvp 1 Reply Last reply Reply Quote 0
      • cvp
        cvp @JasonN last edited by

        @JasonN see

        os.path.basename(path)
        
        1 Reply Last reply Reply Quote 0
        • JasonN
          JasonN last edited by

          Here is what i now have but it still throws an error.

          from sys import argv
          
          from os.path.basename(/private/var/mobile/Library/Mobile Documents/iCloud~com~omz-software~Pythonista3/Documents/kl.py)
          
          script, file_from, file_to = argv
          
          source = open(file_from)
          source_data = source.read()
          
          print "'%s' is %s bytes." % (script, len(source_data))
          
          destination = open(file_to, 'w')
          destination.write(source_data)
          
          source.close()
          destination.close()```
          cvp 1 Reply Last reply Reply Quote 0
          • cvp
            cvp @JasonN last edited by

            @JasonN use as

            import os
            
            script, file_from, file_to = argv
            
            script = os.path.basename(script)
            
            1 Reply Last reply Reply Quote 0
            • JasonN
              JasonN last edited by

              Thank you @cvp

              1 Reply Last reply Reply Quote 0
              • First post
                Last post
              Powered by NodeBB Forums | Contributors