Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows PermissionError in temp file #6

Open
handychimp opened this issue Dec 6, 2021 · 6 comments
Open

Windows PermissionError in temp file #6

handychimp opened this issue Dec 6, 2021 · 6 comments
Labels
bug Something isn't working help wanted Extra attention is needed windows

Comments

@handychimp
Copy link

Hello,

I appear to be encountering an issue with running algorithms from a windows system with a direct input. I've tested this with a number of different sized inputs that are nested lists.

Example input:
test = [ [[127], [128], [129], [130]], [[178], [179], [180], [181], [182], [183], [184], [185]], [[251], [252], [253], [254], [255], [256], [257]] ]

Example call:
spmf = Spmf('GPS', input_direct=test, arguments=[0.5]) spmf.run()
(I've also trialled with 'PrefixScan' algorithm)

Error text (with my actual username replaced):

File "C:\Python39\lib\site-packages\spmf_init_.py", line 46, in init
self.input_ = self.handle_input(
File "C:\Python39\lib\site-packages\spmf_init_.py", line 73, in handle_input
return self.write_temp_input_file(seq_spmf, ".txt")
File "C:\Python39\lib\site-packages\spmf_init_.py", line 87, in write_temp_input_file
os.rename(name, name + file_ending)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\Users\username\AppData\Local\Temp\tmp1qxu06i7' -> 'C:\Users\username\AppData\Local\Temp\tmp1qxu06i7.txt'

Appears to be some issue in trying to modify the file extension without having properly closed the file stream?

@catarinarurbano
Copy link

Hi,

I have exactly the same error when running the algorithms in the Jupyter notebook, did you already solve this problem? It seems impossible to make it work.

@LoLei
Copy link
Owner

LoLei commented Feb 11, 2022

Apparently the file must be closed on Windows before it can be renamed. Try editing the code here yourself. (Install the package via pip install -e . --user from within the cloned git repository to be able to edit the code and have the changes reflected in the installed package immediately.)

If you get it to work you can open a pull request to have your fix merged and released as a new version.

Otherwise I'll push a change myself, but can't say when I'll be able to, also I can't test it on Windows myself.

@LoLei LoLei added bug Something isn't working help wanted Extra attention is needed windows labels Feb 11, 2022
@handychimp
Copy link
Author

Sorry, I should have mentioned: I'd found it was the renaming whilst still open that was the problem too, so instead I wrote the input to a file and called SPMF with a standard input file instead. Have had no further issues when using this method.

It's not required for me to use the temp file, so now I have this code I'm sticking with it for my project. There's no rush needed on a fix. :)

As a question more out of curiosity: once you have to close the temp file, do you not lose any runtime benefit that existed from keeping the stream open? I thought about trying to do it with temp files, but figured if I have to close and reopen the stream, it no longer matters if the input is a temp file or a standard file any more?

Thanks for looking into it!

@LoLei
Copy link
Owner

LoLei commented Feb 11, 2022

Great that a workaround exists, thanks for letting others know.

As to your question, I think I just used the tempfile module for convenience. I'm not aware of any performance differences.

@handychimp
Copy link
Author

handychimp commented Feb 11, 2022

Hi,

I have exactly the same error when running the algorithms in the Jupyter notebook, did you already solve this problem? It seems impossible to make it work.

I have a notification in my email that asked if i could share the code, but it's not in the thread anymore? I'm not sure if it is what you need but, and not necessarily the most elegant but i put them below. They return strings that you will need to write to a text file.

This one is for taking a nested list of sequences, i.e. a list of sequences that are themselves nested lists of form: seq = [ [1, 2, 3] , [1, 5] , [9] ]

def sequence_to_spmf_format(sequence_lists):
    """takes a nested list input and returns a string in SPMF format"""
    seq_spmf = ""
    for seq in sequence_lists:
        for item_set in seq:
            for item in item_set:
                seq_spmf += str(item) + ' '
            seq_spmf += str(-1) + ' '
        seq_spmf += str(-2) + '\n'

    return seq_spmf

In this one, each pattern is a list of strings. I believe most the SPMF algorithms assume no duplicates in the pattern and that each pattern is sorted, so i did that in this function too.

def pattern_to_spmf_format(pattern_list):
    """takes a list of patterns and returns a string in SPMF format"""
    pat_spmf = ""
    for pat in pattern_list:
        p = [str(x) for x in list(set(pat))]
        p.sort()
        pat_spmf += " ".join(p) + "\n"
            
    return pat_spmf

Hope that helps.

@SantoshKumarRaju
Copy link

I was also facing the same issue with temp file on Windows platform, and what I did was to close the temp file before the renaming happens. This seems to have resolved the issue, the updated code is shown below:

    def write_temp_input_file(self, input_text, file_ending):
        tf = tempfile.NamedTemporaryFile(delete=False)
        tf.write(bytes(input_text, 'UTF-8'))
        name = tf.name
        tf.close() # did tf.close before renaming.
        os.rename(name, name + file_ending)
        return name + file_ending

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed windows
Projects
None yet
Development

No branches or pull requests

4 participants