0

I am creating a program where you create a text file and it runs ls in the terminal to display the files that I have in that folder. My problem is that I have 2 folders. One is for the program itself, and one is for the text files that I create. By default, the text file saves to the folder that contains the program. here is my code

#!/usr/bin/python

import time
import termcolor
import os
os.system("cd /home/marc/QuickJotTexts && ls")
print ""

title = raw_input("What Will Your Title Be? ")
print""

print "Your Title Is", title
text_file = open(title, "w")
print ""

text = raw_input("What Do You Want To Write In This Document? ")

text_file.write(text)
text_file.close()

text_file = open(title, "r")

print "YOU WROTE", text_file.read()

nub = raw_input("Is This Correct? y/n ")

if nub == "y":
    print "Ok, Bye :)"
    text_file.close()
if nub == "n":
    os.system("rm", title)

This is only a very small part of my code.

cmd
  • 5,754
  • 16
  • 30

2 Answers2

1

Instead of

text_file = open(title, "w")

which opens a file in your current directory, you should specify what path you want to save in

SAVEDIR = "/home/marc/saves"
text_file = open(os.path.join(SAVEDIR, title), "w")
cmd
  • 5,754
  • 16
  • 30
0

os.system() executes independently of your python interpreter. The interpreter doesn't actually change its working directory with that command, but rather fires it off as a background process and reports the exit code (hopefully, '0').

What you're looking for is os.chdir().

Your question is adequately addressed in this SO article.

Community
  • 1
  • 1
Edwin S.
  • 146
  • 1
  • 5