Results 1 to 7 of 7

Thread: Python file management

  1. #1
    Join Date
    Nov 2011
    Beans
    8

    Python file management

    Hi all,
    I'm using IDLE (and python 3) to try and open a (.doc) file with this script:

    my_file = open('/home/jack/Documents/Python/PFMgmt')
    open_file = my_file.read()
    print(open_file)

    ... and it doesn't work. I get the message (in the Python shell):

    File "/home/jack/Documents/Python/py files/File mgt practice.py", line 3, in <module>
    my_file = open('/home/jack/Documents/Python/PFMgmt')
    IOError: [Errno 2] No such file or directory: '/home/jack/Documents/Python/PFMgmt'

    I know the script works as I can use it to open a text file in /etc, no problem.

    I've checked in the absolute file path in the terminal, it looks to be correct. I know the file is there because I put it there, I can see it there in terminal and GUI, it is bloody well there!

    Can anybody please tell me why this isn't working!?


    Thanks very much for any advice.

    Jack.

  2. #2
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,588
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: Python file management

    Quote Originally Posted by jaquesaulait View Post
    Hi all,
    I'm using IDLE (and python 3) to try and open a (.doc) file with this script:
    If it is a .doc file, then isn't its name /home/jack/Documents/Python/PFMgmt.doc?

  3. #3
    Join Date
    Nov 2011
    Beans
    8

    Re: Python file management

    Hi SP,
    Thanks for the reply. I had tried that to no avail; and the text files that have opened didn't require the (.txt) file extension. However, I tried adding .doc to my script and got this error message in the shell:

    File "/home/jack/Documents/Python/py files/File mgmt ex II.py", line 4, in <module>
    open_file = my_file.read()
    File "/usr/lib/python3.2/codecs.py", line 300, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
    UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in position 0: invalid continuation byte

    At least it's not saying, 'no such file', now. But I cannot see what is wrong with the line with the reported error, and, indeed, this script still works on an /etc file.

    Any ideas..?

    Thanks, Jack.




  4. #4
    Join Date
    Apr 2012
    Beans
    7,256

    Re: Python file management

    What are you trying to do, exactly? If these are Word (or Word-compatible) .doc files then their raw content isn't going to be printable in the sense that python's 'print' function understands - you'd need to open them in binary mode and then do some pretty serious processing to extract any printable text, I think

    If they are .docx then that might be a little easier since they are compressed archives with actual content in XML (and python has XML parser modules)

    Or are you trying to send documents to an actual printer, using python?

  5. #5
    Join Date
    Nov 2011
    Beans
    8

    Re: Python file management

    I'm trying to write some text into a file using Python scripts, for which my learning materials has instructions. My original file has the text, 'Alpha' in it ... I'm trying to add the text, 'Beta' ... it's not working. Some small progress: I can now write to this file: /home/jack/File mgmt prac (.txt)
    .. but the added text does not save to the original file..! So far, only 1 extra file with the added text has appeared in the folder, with the added text, Beta, on its own. It's a mystery. And / or I can see the added text only when the file is opened (with the same script that adds the text) in the Python shell. (The .docx file wouldn't open in the shell, either.) I think my learning materials are incomplete - what is the point of writing to a file if you can't save it?

  6. #6
    Join Date
    Nov 2011
    Beans
    8

    Re: Python file management

    Well, I'm no further forward, it'll just have to remain a mystery. Thanks anyway to those who took the time to respond.

  7. #7
    Join Date
    Feb 2009
    Beans
    1,469

    Re: Python file management

    You had a file named (presumably) /home/jack/File mgmt prac.txt, containing "Alpha".

    You used Python to write to (presumably) /home/jack/File mgmt prac the text "Beta".

    Result: You now have two files, one with the .txt extension and one without, containing different data.

    No mystery, just an oversight.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •