Sam
Sam

Reputation: 2200

Python: Dynamically add relative path, based on OS

I wrote my first program in Python for my dad to convert about 1000 old AppleWorks files that he has (the AppleWorks format, .cwk, is no longer supported) to .docx. To clarify, the program does not actually convert anything, all it does is copy/paste whatever text is in the documents you specify to another document of any file-type you want.

The program works fine on my Windows Laptop, however it encounters problems in my dad's Mac laptop.

File-path in Windows is indicated with a \ whereas in Mac it's /. So when the program reaches the copy and paste variables it stops working if the slash in the respective string is the wrong way around.

Is there a way to get Python to dynamically add on the Input and Output folders to my copy and paste variables depending on the OS without the use of strings?

If there are any other improvements you can see, feel free to say so, I am interested in releasing this as Freeware, possibly with the tKinter GUI and want to make as user friendly as possible.

As it stands the program does have some problems (converting apostrophe's into omega symbols and the like). Feel free to try the program and see if you can improve it.

import os, os.path
import csv
from os import listdir
import sys
import shutil

path, dirs, files = os.walk(os.getcwd() + '/Input').next()

file_count = len(files)
if file_count > 0:
  print "There are " + str(file_count) + " files you have chosen to convert."
else:
  print "Please put some files in the the folder labelled 'Input' to continue."
ext = raw_input("Please type the file extension you wish to convert to, making sure to     preceed your selection with '.' eg. '.doc'")

convert = raw_input("You have chosen to convert " + str(file_count) + " files to the "     + ext + " format. Hit 'Enter' to continue.")
if convert == "":
  print "Converter is now performing selected tasks."
  def main():
    dirList = os.listdir(path)
    for fname in dirList:
      print fname
      # opens files at the document_input directory.
      copy = open(os.getcwd() + "\Input\\" + fname, "r")
      # Make a file called test.docx and stick it in a variable called 'paste'
      paste = open(os.getcwd() + "\Output\\" + fname + ext, "w+")
      # Place the cursor at the beginning of 'copy'
      copy.seek(0)
      # Copy all the text from 'copy' to 'paste'
          shutil.copyfileobj(copy,paste)
          # Close both documents
          copy.close()
          paste.close() 
      if __name__=='__main__':
        main()
    else:
      print "Huh?"
      sys.exit(0)

Please let me know if I wasn't clear or left out some info...

Upvotes: 2

Views: 9938

Answers (2)

Blender
Blender

Reputation: 298106

os.path.join is the platform-independent way to join paths:

>>> os.path.join(os.getcwd(), 'Input', fname)
'C:\\Users\\Blender\\Downloads\\Input\\foo.txt'

And on Linux:

>>> os.path.join(os.getcwd(), 'Input', fname)
'/home/blender/Downloads/Input/foo.txt'

Upvotes: 2

sapi
sapi

Reputation: 10224

Use os.path.join

For example, you could get the path of the Input subdirectory with

path = os.path.join(os.getcwd(), 'Input')

Upvotes: 7

Related Questions