0
votes

I am having trouble looping over this function, resulting in an error that implies the file still being used as follows:

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\Will\\Dropbox\\Python\\portfolio\\_temp\\15219045498121354237513144543229969014.wav'

I believe this is due to the file still being deleted while i am trying to rename another file to the same name as the one being deleted. Is this accurate? and how to I solve such an issue?

def splice(filepath: str, start, duration, save_as: str):
    """
    @param filepath: absolute filepath to wav file
    @param start: startime in seconds/str/timestamp
    @param duration: duration in seconds/str/timestamp
    @param save_as: filepath for new file
    """
    # initialize
    if not save_as: save_as = filepath
    filepath = os.path.abspath(filepath)
    save_as = os.path.abspath(save_as)
    if filepath[-4:] != '.wav': filepath += '.wav'
    if save_as[-4:] != '.wav': save_as += '.wav'
    if filepath == save_as:
        save_as = os.path.join(os.path.dirname(filepath),
                               '_temp_'+str(random.getrandbits(128))+'.wav')

    # perform operation
    start = audio_tools.convert.timestamp_to_seconds(start)
    duration = audio_tools.convert.timestamp_to_seconds(duration)
    result = os.system("ffmpeg -y -ss " + str(start) + \
              " -t " + str(duration) + \
              " -i \"" + filepath + "\"" + \
              " \"" + save_as + "\"")
    assert result == 0, "Error in creating file"

    # delete original and rename temp file 
    if '_temp_' in os.path.basename(save_as):
        os.remove(filepath)
        os.rename(save_as,filepath)
1

1 Answers

1
votes

The reason for my issue is that I was using a dropbox directory as my working directory. This caused conflicts.